Switch Statement in JavaScript with example

 A switch statement allows you to evaluate multiple conditions, checking for specific values.


Example:


let day = 3;

let dayName;


switch (day) {

    case 1:

        dayName = "Monday";

        break;

    case 2:

        dayName = "Tuesday";

        break;

    case 3:

        dayName = "Wednesday";

        break;

    default:

        dayName = "Unknown Day";

}


console.log(dayName); // Output: Wednesday

Explanation:

The switch statement checks the value of day.
If day is 3, it assigns "Wednesday" to dayName.

No comments:

Post a Comment

Angular's new httpResource and a bonus hidden feature

  The first   release candidate   for   Angular 19.2   is out, and it includes the new experimental   httpResource . This is essentially a s...

Best for you