JavaScript Decision Making

Decision making is a fundamental aspect of programming that allows you to control the flow of your code by making choices based on conditions. In JavaScript, you can use various constructs to implement decision-making logic. In this tutorial, we'll explore how to make decisions in JavaScript.  

The if Statement

The if statement is one of the most basic decision-making constructs in JavaScript. It allows you to execute a block of code only if a specified condition is true.

if (condition) {

    // Code to be executed if the condition is true

}

For example, if you want to check if a user is older than 18:

let age = 20;


if (age > 18) {

    console.log("You are an adult.");

}

The if...else Statement

The if...else statement allows you to execute one block of code if a condition is true and another block if the condition is false. 

if (condition) {

    // Code to be executed if the condition is true

} else {

    // Code to be executed if the condition is false

}

For example, to check if a user is eligible to vote:

let age = 16;

if (age >= 18) {
    console.log("You can vote.");
} else {
    console.log("You cannot vote yet.");
}

The if...else if...else Statement

The if...else if...else statement allows you to test multiple conditions in sequence and execute the code block associated with the first true condition.  

if (condition1) {
    // Code to be executed if condition1 is true
} else if (condition2) {
    // Code to be executed if condition2 is true
} else {
    // Code to be executed if no condition is true
}

For example, classifying exam grades:

let score = 75;

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

Ternary Operator

The ternary operator (condition ? expression1 : expression2) provides a concise way to make decisions and assign values based on a condition. 

let isSunny = true;
let weatherMessage = isSunny ? "It's sunny!" : "It's not sunny.";
console.log(weatherMessage);

The switch Statement

The switch statement is used when you have a single expression to be compared to multiple possible case values. It provides an efficient way to perform different actions based on the value of the expression.

switch (expression) {
    case value1:
        // Code to be executed if expression === value1
        break;
    case value2:
        // Code to be executed if expression === value2
        break;
    // More cases...
    default:
        // Code to be executed if no case matches
}

For example, displaying the day of the week:

let day = "Monday";

switch (day) {
    case "Monday":
        console.log("It's the start of the week.");
        break;
    case "Friday":
        console.log("It's almost the weekend.");
        break;
    default:
        console.log("It's an ordinary day.");
}

Conclusion 

Decision-making is a critical part of programming in JavaScript. By using if statements, switch statements, and the ternary operator, you can control the flow of your code and make it responsive to various conditions.