JavaScript Series: Everything You Need To Know About JavaScript Conditionals
4 min readOct 21, 2021
Drama starts where logic ends — Ram Charan
Have you ever driven a car without a steering wheel?
That’s exactly how code behaves if you removed all the logical conditionals out of the program.
Or worse, if you assume their behavior.
Conditionals are code blocks that allow you to test a condition and return either true or false (Boolean values).
Then will execute/skip a piece of code based on the result.
if (condition) {
// Execute this if condition is true
// Skip this code if condition is false
}
// This will execute because is outside the condition
For example:
let name = 'Jessica';
let age = 25;
let isEmployed = true;// If statement
if (name === 'Jessica'){
// Some code
}// if-else statements
if (age < 18){
// Print 'Too Young for Master Degree'
} else{
// Print 'Can apply for Master Degree'
}// Ternary operator
isEmployed ? // Print 'Congratulations!' : // Print 'Keep trying'
Remember to use braces for all control structures (i.e. if
, else
, for
, do
, while
) even if the body contains only a…