DEV Community

Cover image for Decision Making in JavaScript
Kesavarthini
Kesavarthini

Posted on

Decision Making in JavaScript

🔀 Decision Making in JavaScript
Programs need to take decisions based on conditions, and JavaScript provides multiple ways to do that.

📌 1. if, else, else if

These are used to execute code based on conditions.

🔹 if Statement

if statement executes a block of code when a condition is true

let age = 18;

if (age >= 18) {
    console.log("You are eligible to vote");
}
Enter fullscreen mode Exit fullscreen mode

Nested if
You can use an if statement inside another if statement:

let age = 16;
let country = "USA";

if (country == "USA") {
  if (age >= 16) {
    console.log("You can drive!");
  }
}
Enter fullscreen mode Exit fullscreen mode

🔹 if-else Statement
Use the else statement to specify a block of code to be executed if a condition is false.

let age = 16;

if (age >= 18) {
    console.log("Eligible");
} else {
    console.log("Not Eligible");
}

Enter fullscreen mode Exit fullscreen mode

🔹 The else if Statement
Use the else if statement to specify a new condition if the first is false.

let marks = 75;

if (marks >= 90) {
    console.log("Grade A");
} else if (marks >= 60) {
    console.log("Grade B");
} else {
    console.log("Grade C");
}
Enter fullscreen mode Exit fullscreen mode

📌 2. switch Statement

  • Used when we have multiple conditions based on a single value.
  • switch is often used as a more readable alternative to many if...else if...else statements, especially when dealing with multiple possible values.
let day = 2;

switch(day) {
    case 1:
        console.log("Monday");
        break;
    case 2:
        console.log("Tuesday");
        break;
    case 3:
        console.log("Wednesday");
        break;
    default:
        console.log("Invalid day");
}
Enter fullscreen mode Exit fullscreen mode

This is how it works:

  • The switch expression is evaluated once.
  • The value of the expression is compared with the values of each case.
  • If there is a match, the associated block of code is executed.
  • If there is no match, no code is executed.

The break Keyword

  • When JavaScript reaches a break keyword, it breaks out of the switch block.
  • This will stop the execution inside the switch block.
  • No more statements in the switch block will be executed.

  • The break keyword is crucial for preventing a "fall-through."

  • Without break, the code will continue to execute the next case blocks (and the default block if present) even if their values do not match the expression.

📌 3. Truthy and Falsy Values
In JavaScript, values are treated as true or false in conditions.

🔹 Truthy Values
👉 Everything else is truthy.

Examples:

  • Non-zero numbers: 42, -1, 3.14
  • Non-empty strings: "hello", "0", " "
  • Objects and arrays: {}, []
  • Functions: function() {}
  • Dates: new Date()
  • Symbols: Symbol()
  • BigInt values other than 0n: 10n

🔹 Falsy Values

  • false
  • 0 (and -0)
  • 0n (BigInt zero)
  • "" (empty string)
  • null
  • undefined
  • NaN

🔹 Example

let name = "";

if (name) {
    console.log("Name is present");
} else {
    console.log("Name is empty");
}
Enter fullscreen mode Exit fullscreen mode

Output:

Name is empty
Enter fullscreen mode Exit fullscreen mode

Top comments (0)