Logic, also known as conditionals, allows us to control which parts of our code get executed in which situations. The most basic form is the if statement.
<h1>Hello World<h1>
const happyCoder =true;
if(happyCoder ===true){
console.log('Coder is happy');
}
if(happyCoder ===false){
console.log('Coder is not happy');
}
We use boolean operators such as === or <= to determine if a condition is true or false and execute the relevant code.
AND
We can check multiple statements with the 'AND' operator &&
<h1>Hello World<h1>
const temperature =28;
if(temperature >28){
console.log('Coder is too hot');
}
if(temperature >15&& temperature <=28){
console.log('Coder is just right');
}
if(temperature <=15){
console.log('Coder is too cold');
}
In an 'AND' check each condition being checked much be true for the overall condition to be considered true.
OR
If we want to execute code based on if one, or more, of any set of conditions are true, then we use the OR operator ||
To save us having to code for each version of a set of conditions, we can simply code an if and pair it with an else to catch the inverse of that check.