Control Flow: Conditionals (if/else)

Master conditional statements (if, else if, else) to control the flow of your code based on different conditions.


JavaScript Conditionals

Control Flow: Conditionals (if/else)

In JavaScript, control flow refers to the order in which your code is executed. Conditional statements allow you to control this flow by executing different blocks of code based on whether a certain condition is true or false. The most fundamental conditional is the if/else statement.

The if statement executes a block of code only if a specified condition is true. If the condition is false, the code block is skipped. The optional else statement provides an alternative block of code to execute when the if condition is false.

Syntax

 if (condition) {
  // Code to execute if the condition is true
} else {
  // Code to execute if the condition is false (optional)
} 

Example

 let age = 20;

if (age >= 18) {
  console.log("You are an adult.");
} else {
  console.log("You are a minor.");
} 

In the example above, the condition age >= 18 is evaluated. Since age is 20, the condition is true, and the message "You are an adult." is printed to the console.

An if statement can also stand alone without an else clause:

 let isRaining = true;

if (isRaining) {
  console.log("Bring an umbrella!");
} 

Master Conditional Statements (if, else if, else)

The if, else if, and else statements provide a more sophisticated way to handle multiple conditions. The else if statement allows you to check additional conditions if the initial if condition is false. You can have multiple else if statements. The final else statement (optional) is executed if none of the preceding conditions are true.

Syntax

 if (condition1) {
  // Code to execute if condition1 is true
} else if (condition2) {
  // Code to execute if condition1 is false AND condition2 is true
} else if (condition3) {
  // Code to execute if condition1 and condition2 are false AND condition3 is true
} else {
  // Code to execute if all preceding conditions are false (optional)
} 

Example

 let score = 75;

if (score >= 90) {
  console.log("Grade: A");
} else if (score >= 80) {
  console.log("Grade: B");
} else if (score >= 70) {
  console.log("Grade: C");
} else if (score >= 60) {
  console.log("Grade: D");
} else {
  console.log("Grade: F");
} 

In this example, the code checks the value of score against a series of conditions. Since score is 75, the third else if condition (score >= 70) is true, and the message "Grade: C" is printed to the console. Only the first matching condition is executed.

Important Notes:

  • The conditions are evaluated in order, from top to bottom.
  • Only the code block associated with the first true condition is executed. Subsequent conditions are not evaluated.
  • The else block, if present, is executed only if none of the preceding if or else if conditions are true.
  • You can nest if statements within other if or else statements to create more complex logic.