Decision making in Javascript

Rumman Ansari   Software Engineer   2023-03-26   5635 Share
☰ Table of Contents

Table of Content:


Decision making in JavaScript is done using conditional statements. The most commonly used conditional statements in JavaScript are the if statement, the else if statement, and the else statement. These statements allow you to specify different code blocks to execute based on different conditions.

Conditions - If, Else, Else If

Syntax:


    if (condition 1)
    {        
     Execute code if condition 1 is true
    }
    else if (condition 2)
    {        
      Execute code if condition 2 is true
    }
    else
    {        
      Execute code if both conditions are false
    }
 
Conditions - Switch

Syntax:


 switch(expression)
 {
        case 1:
            code to execute
            break;
        case 2:
            code to execute
            break;
        case 3:
            code to execute
            break;
        default:
            code to execute   
 } 

Here's an example of an if statement in JavaScript:


var num = 10;

if (num > 0) {
  console.log("The number is positive.");
}

In this example, the if statement checks whether the value of num is greater than 0. If the condition is true, the code block inside the if statement is executed, which prints the message "The number is positive." to the console.

Here's an example of an if...else statement in JavaScript:


var num = -5;

if (num > 0) {
  console.log("The number is positive.");
} else {
  console.log("The number is not positive.");
}

In this example, the if statement checks whether the value of num is greater than 0. If the condition is true, the code block inside the if statement is executed, which prints the message "The number is positive." to the console. If the condition is false, the code block inside the else statement is executed, which prints the message "The number is not positive." to the console.

Here's an example of an if...else if...else statement in JavaScript:


var num = 0;

if (num > 0) {
  console.log("The number is positive.");
} else if (num < 0) {
  console.log("The number is negative.");
} else {
  console.log("The number is zero.");
}

In this example, the if statement checks whether the value of num is greater than 0. If the condition is true, the code block inside the if statement is executed, which prints the message "The number is positive." to the console. If the condition is false, the else if statement checks whether the value of num is less than 0. If the condition is true, the code block inside the else if statement is executed, which prints the message "The number is negative." to the console. If both conditions are false, the code block inside the else statement is executed, which prints the message "The number is zero." to the console.

There are also other types of conditional statements in JavaScript, such as the switch statement and the ternary operator (?:). However, the if statement and its variants are the most commonly used ones.