Control Flow Statements in JavaScript
JavaScript·25 min read·Jan 1, 2026
In programming, control flow statements are code structures used to manage and alter the standard execution flow and logic of a script by conditionally executing (or not) a set of statements based on predicates (or expressions) called conditions.
Comparison operators
A condition is an expression that when evaluated produces a boolean result that's either true or false.
These conditions are created using comparison operators that allow to logically compare values between them.
Checking the equality of values
In JavaScript, the equality or inequality of two values can be verified using the loose equality, strict equality, inequality, and strict inequality operators.
Loose equality
When using the loose equality operator ==, JavaScript performs a type coercion, which is an implicit type conversion of values from one data type to another (e.g., string to number), and checks whether its operands values are equal.
By doing this, JavaScript attempts to make sense of the comparison by converting one or both values to a common type before executing it.
In this context, JavaScript may treat different data types as equal if their values are equivalent after conversion.
For example, all of the following comparisons will output true:
console.log(18 == '18');
console.log(false == 0);
console.log(true == 1);
console.log(null == undefined);
console.log(0 == '');
Strict equality
When using the strict equality operator ===, JavaScript doesn't perform type coercion and checks whether its operands types and values are equal.
For example, all of the following comparisons will output false:
console.log(18 === '18');
console.log(false === 0);
console.log(true === 1);
console.log(null === undefined);
console.log(0 === '');
Inequality
When using the inequality operator !=, JavaScript performs a type coercion and checks whether its operands values are not equal.
For example, all of the following comparisons will output false:
console.log(18 != '18');
console.log(false != 0);
console.log(true != 1);
console.log(null != undefined);
console.log(0 != '');
Strict inequality
When using the strict inequality operator !==, JavaScript doesn't perform type coercion and checks whether its operands types and values are not equal.
For example, all of the following comparisons will output true:
console.log(18 !== '18');
console.log(false !== 0);
console.log(true !== 1);
console.log(null !== undefined);
console.log(0 !== '');
Compare numbers
To compare numbers, you can use:
- The
>operator to check if a number is greater than another. - The
<operator to check if a number is lesser than another. - The
>=operator to check if a number is greater or equal to another. - The
<=operator to check if a number is lesser or equal to another.
For example, all of the following comparisons will output true:
console.log(10 > 5);
console.log(5 < 10);
console.log(10 >= 5);
console.log(5 <= 10);
Control flow statements
In JavaScript, conditional expressions are usually combined with control flow statements in charge of altering the execution logic of the script, and determining whether certain code blocks should or shouldn't be executed.
The if statement
The if statement is used to execute a set of statements based on the boolean evaluation of a condition.
If the condition specified between the parentheses () evaluates to true, the statements specified between the curly brackets {} are executed; otherwise, they are ignored.
if (condition) {
statements
}
Example
Let's consider this script:
const age = 21;
if (age >= 18) {
console.log('Have a beer 🍺');
}
When executed, it will:
- Define a variable named
ageand initialize it with21. - Check if the value of the
agevariable is greater or equal to18. - Output the string
"Have a beer 🍺"if the condition evaluates totrue.
Which will produce this output:
Have a beer 🍺
The else statement
The else statement, always used in conjunction with the if statement, is used to define an alternative set of statements that should be executed whenever the condition specified in the if statement evaluates to false.
if (condition) {
statements
} else {
statements
}
Example
Let's consider this script:
const age = 16;
if (age >= 18) {
console.log('Have a beer 🍺');
} else {
console.log('Have a juice 🧃');
}
When executed, it will:
- Define a variable named
ageand initialize it with16. - Check if the value of the
agevariable is greater or equal to18. - Output the string
"Have a beer 🍺"if it evaluates totrue. - Output the string
"Have a juice 🧃"if it evaluates tofalse.
Which will produce this output:
Have a juice 🧃
Tip: Manually change the value of the
agevariable to modify the behavior of the script.
The else if statement
The else if statement, always used in conjunction with the if statement, is used to test multiple conditions sequentially and execute different sets of statements based on the first condition that evaluates to true.
if (condition) {
statements
} else if (condition) {
statements
}
Note: You can also add a final
elsestatement after theelse ifstatement if you want to specify a set of fallback statements in case none of the previous conditions are met.if (condition) { statements } else if (condition) { statements } else { statements }
Example
Let's consider this script:
const country = 'France';
let speciality;
if (country === 'Canada') {
speciality = 'Poutine';
} else if (country === 'France') {
speciality = 'Pot-au-feu';
} else {
speciality = 'Unknown';
}
console.log(speciality);
When executed, it will:
- Define a variable named
countryand initialize it with the string"France". - Define an undefined variable named
speciality. - Check if the value of the
countryvariable equals the string"Canada"and reassign the string"Poutine"to thespecialityvariable if it evaluates totrue. - Check if the value of the
countryvariable equals the string"France"and reassign the string"Pot-au-feu"to thespecialityvariable if it evaluates totrue. - Otherwise, reassign the string
"Unknown"to thespecialityvariable if both conditions above evaluate tofalse. - Output the value of the
specialityvariable.
Which will produce this output:
Pot-au-feu
Tip: Manually change the value of the variable
countryto modify the behavior of the script.
The switch statement
The switch statement is used to evaluate a single variable or expression against a list of values called clauses.
It will execute all the statements after the first case clause with a matching value, until a break statement is encountered.
switch (expression) {
case value:
statements
break;
case value:
statements
break;
// ...
}
Just like with the else statement, it is possible to define an optional default fallback case, whose statements will be executed if none of the above case statements match a value:
switch (expression) {
case value:
statements
break;
// ...
default:
statements
}
Notes:
The
breakstatement in thedefaultclause is optional.The
switchstatement allows to regroup multiplecasestatements to match multiple values at once:switch (expression) { case value: case value: statements break; }
Example
Let's consider this script:
const brand = 'Honda';
let country;
switch (brand) {
case 'Yamaha':
case 'Honda':
country = 'Japan';
break;
case 'Triumph':
country = 'England';
break;
default:
country = 'Unknown';
}
console.log(country);
When executed, it will:
- Define a variable named
brandand initialize it with the string"Honda". - Define an undefined variable named
country. - Check if the value of the
brandvariable either equals the string"Yamaha"or"Honda"and reassign the string"Japan"to thecountryvariable if it evaluates totrue. - Check if the value of the
brandvariable equals the string"Triumph"and reassign the string"England"to thecountryvariable if it evaluates totrue. - Otherwise, reassign the string
"Unknown"to thecountryvariable if both cases above evaluate tofalse. - Output the value of the
countryvariable.
Which will produce this output:
Japan
Tip: Manually change the value of the
brandvariable to modify the behavior of the script.
Nest conditional statements
A nested conditional statement is a statement placed inside another statement to create complex decision-making structures, allowing you to evaluate multiple conditions and execute different statements based on these conditions.
Just like with any other statements, each inner statement is executed based on the outcome of the outer statement, and so on.
Nest if..else statements
When placing an if statement within another if statement, the second statement will only be evaluated if the first one evaluates to true.
if (condition) {
if (condition) {
statements
}
}
When placing an if statement within an else statement, the second statement will only be evaluated if the first one evaluates to false.
if (condition) {
statements
} else {
if (condition) {
statements
}
}
Note that, in this case, the above structure is equivalent to the following one:
if (condition) {
statements
} else if (condition) {
statements
}
Example
Let's consider this script:
const brand = 'Honda';
const model = 'CB500X';
if (brand === 'Honda') {
if (model === 'CB500X') {
console.log('Trail');
} else {
console.log('Unknown model');
}
} else {
console.log('Unknown brand');
}
When executed, it will:
- Define a variable named
brandand initialize it with the string"Honda". - Define a variable named
modeland initialize it with the string"CB500X". - Check if the value of the
brandvariable equals the string"Honda"or output the string"Unknown brand". - Check if the value of the
modelvariable equals the string"CB500X"and output the string"Trail", or output the string"Unknown model"otherwise.
Which will produce this output:
Trail
Nest switch statements
Nesting a switch statement is quite similar to nesting other statements, as it will only be executed if the first variable matches the pattern it is defined under.
switch (expression) {
case value:
switch (expression) {
case value:
statements
break;
// ...
default:
statements
break; // Additional break
}
break;
case value:
statements
break;
// ...
default:
statements
}
Note: When using the
defaultcase in a nestedswitchstatement, its set of statements must imperatively be followed by abreakstatement.
Example
Let's consider this script, with an identical logic to the previous one:
const brand = 'Honda';
const model = 'CB500X';
switch (brand) {
case 'Honda':
switch (model) {
case 'CB500X':
console.log('Trail');
break;
default:
console.log('Unknown model');
break;
}
break;
default:
console.log('Unknown brand');
}
Which will produce this output:
Trail
🗒️ Summary
Here's a summary of what you've learned in this lesson:
- The
==operator is used to check the equality of two type coerced values. - The
===operator is used to check the equality of two values. - The
!=operator is used to check the inequality of two type coerced values. - The
!==operator is used to check the inequality of two values. - The
>operator is used to check if a number is greater than another. - The
<operator is used to check if a number is lesser than another. - The
>=operator is used to check if a number is greater or equal to another. - The
<=operator is used to check if a number is lesser or equal to another. - The
ifstatement is used to execute a set of statements based on the truthy evaluation of a condition. - The
elsestatement is used to define a set of statements that should be executed when the condition specified in the if statement evaluates to false. - The
else..ifstatement, in conjunction with the if statement, is used to test multiple conditions sequentially and execute different sets of statements based on the first condition that evaluates to true. - The
switchstatement is used to evaluate a single variable or expression against a list of values called clauses.