JavaScript Series: Everything You Need To Know About JavaScript Conditionals

Renato Francia
4 min readOct 21, 2021
Photo by Evan Dennis on Unsplash

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 single statement.

Comparison Operators

Comparison operators evaluate statements to determine equality or difference of variables.

==       // equal to
=== // equal value and equal type
!= // not equal to
!== // not equal value and equal type
> // greater than
< // less than
>= // greater than or equal to
<= // less than or equal to

Logical Operators

Logical operators are typically used with Boolean (logical) values and return a true or false as a result.

AND ( && )

expr1 && expr2

Only returns true if all the expressions return true. Otherwise, it returns false

a1 = true  && true       // t && t returns true
a2 = true && false // t && f returns false
a3 = false && true // f && t
Renato Francia

Software Developer, Digital Nomad, Blogger and Tech Enthusiast.