if and if else statements
We can make decisions in our code using if and if else statements. It is very much like this template:
if *some condition is true*, then *a certain action will happen*, else *another action will happen*
For example, if it is raining then, I will take my umbrella, else I will leave my umbrella at home. It is not that much different in code:
let rain = true;
if(rain){
console.log("** Taking my umbrella when I need to go outside **");
} else {
console.log("** I can leave my umbrella at home **");
}
In this case, the value of rain
is true
. And therefore, it will log to the console:
** Taking my umbrella when I need to go outside **
But let's first take a step back and look at the syntax. We start with the word "if." After this, we get something within parentheses. Whatever is between these parantheses will be translated to a Boolean. If the value of this Boolean is true
, it will execute the block...