What the heck is an if/else statement?

If else statement is one of the integral part of virtually all programming languages that plays the basic role of decision making by using code embedded in a conditional phrase. In simpler term, If/else statements are conditions that allow us make decisions using code.

Practical illustration on If/else statement.

Take for example, you intend to buy a carpet for your room and you want to choose the colour base on that of your Curtains or Table set which you haven't gotten either.

The way your brain process that shouldn't differ from the following;

If I happen to buy a Red šŸ”“Table, I'm definitely buying a Blue(sky) šŸ”µCarpet cos both colours seem complimentary. Or if I was able to get a green curtain, I'll opt for an orange coloured šŸ”¶carpet. Or even set a condition that encapsulate the both arguments i.e( Table colour and Curtain colours ) which will make you buy a contrasting colour to both(Red and Green) probably Grey . And as humans,not every of our wishes come to realization. We naturally can't eventuate everything ,so we can have it at the back of our mind that, If at all I'm not buying a Red Table set and a green curtain I must buy a carpet cos of it it's utmost importance.. then I'm buying Pink šŸ½regardless of anything.

So this is how the brain process the decisions. We can likewise fork something of such using block of codes /programming language to express it. And we'll be doing that below.

Mind you, your knowledge of variables, data types and the likes would be of great use here.

Let's get started.

In JavaScript you start to make decisions firstly with the use of a conditional statement i.e if then followed by a parentheses () where your logical conditions go into , and thereafter open a curly braces {} and pass your block of code.. what to be executed within it.

With this let's replicate the above decision using JavaScript if/else statement.

First and foremost, we need to store our basic needs into a variable (container) in other to reuse them often and often. So let's do that.

var isRed = 'Red' ;
var Table
/* for the first one , if i buy a red 
I'm def buying a blue carpet.
if (Table === isRed) {
console.log('Blue carpet gotten')
var isGreen = true; // Boolean logic
var Curtain;
var carpet /*remember carpet dosen't have 
a definition yet cos we're buying it based
on the Table or/and Curtain)
else if (Curtain === isGreen) {
console.log('Orange carpet bought!')
// considering the Table and Curtain together
else if(Table === isRed && Curtain === isGreen ){
   console.log('I got grey this time around')
   console.log('Pink all the way , carpet is carpet !! ')

Else is used when we intend to conclude our decision and we felt like having a fallback decision (a plan B if everything doesn't work out as planned) so if we have another decision to make after the first if , we'd firstly write else then followed by if to show it's a second fiddle to the first condition. And it goes on like that till we reach our last decision where we introduce a fallback condition(plan B) with else.

In the above code blocks, we explored some features that hasn't been previously explained, like introducing else , and some other places writing else if together, use of === (strict operator) , && (And) logic etc. So let's take them in turns.

===(strict operator) is used to set an equality condition which maps the value and data type together before it can be passed to be true.. would be discussed extensively in subsequent lectures.

We made use of &&( AND) operators, this would lead us to discussing &&(AND) and || (OR) extensively.

If, which I believe we have in one way come across Truth table in Mathematics, we would recollect that there's a section for And and Or gate which defines when the condition would be true and false for execution, this same principle is applicable in JavaScript where it's used as to draw comparison between two conditions and ultimately used to execute our decisions based on truthy and falsey parsing.

Below are diagrammatic illustration of And , Or and Not table with application.