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:

  1. Define a variable named age and initialize it with 21.
  2. Check if the value of the age variable is greater or equal to 18.
  3. Output the string "Have a beer 🍺" if the condition evaluates to true.

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:

  1. Define a variable named age and initialize it with 16.
  2. Check if the value of the age variable is greater or equal to 18.
  3. Output the string "Have a beer 🍺" if it evaluates to true.
  4. Output the string "Have a juice 🧃" if it evaluates to false.

Which will produce this output:

Have a juice 🧃

Tip: Manually change the value of the age variable 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 else statement after the else if statement 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:

  1. Define a variable named country and initialize it with the string "France".
  2. Define an undefined variable named speciality.
  3. Check if the value of the country variable equals the string "Canada" and reassign the string "Poutine" to the speciality variable if it evaluates to true.
  4. Check if the value of the country variable equals the string "France" and reassign the string "Pot-au-feu" to the speciality variable if it evaluates to true.
  5. Otherwise, reassign the string "Unknown" to the speciality variable if both conditions above evaluate to false.
  6. Output the value of the speciality variable.

Which will produce this output:

Pot-au-feu

Tip: Manually change the value of the variable country to 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 break statement in the default clause is optional.

  • The switch statement allows to regroup multiple case statements 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:

  1. Define a variable named brand and initialize it with the string "Honda".
  2. Define an undefined variable named country.
  3. Check if the value of the brand variable either equals the string "Yamaha" or "Honda" and reassign the string "Japan" to the country variable if it evaluates to true.
  4. Check if the value of the brand variable equals the string "Triumph" and reassign the string "England" to the country variable if it evaluates to true.
  5. Otherwise, reassign the string "Unknown" to the country variable if both cases above evaluate to false.
  6. Output the value of the country variable.

Which will produce this output:

Japan

Tip: Manually change the value of the brand variable 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:

  1. Define a variable named brand and initialize it with the string "Honda".
  2. Define a variable named model and initialize it with the string "CB500X".
  3. Check if the value of the brand variable equals the string "Honda" or output the string "Unknown brand".
  4. Check if the value of the model variable 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 default case in a nested switch statement, its set of statements must imperatively be followed by a break statement.

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 if statement is used to execute a set of statements based on the truthy evaluation of a condition.
  • The else statement 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..if statement, 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 switch statement is used to evaluate a single variable or expression against a list of values called clauses.