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

JavaScript + Angular-compatible version of loan amortization calculator that you can integrate into an Angular component or service

  JavaScript Version of Loan Amortization 1. Loan Calculator Function (Pure JS/TS) export function calculateLoanSchedule ( principal:...

Best for you