Member-only story
JavaScript switch case vs object literal

If you’re on a free Medium plan, click here to read — Free Access
For more questions and answers visit our website at Frontend Interview Questions
What is a switch
Case Statement in JavaScript?
A switch
case statement in JavaScript is a control structure that allows you to test a variable or expression against multiple values. When a match is found, the corresponding block of code is executed. This is often used when you have multiple conditions based on the same variable.
Syntax:
switch(expression) {
case value1:
// Code to execute if expression matches value1
break;
case value2:
// Code to execute if expression matches value2
break;
default:
// Code to execute if none of the values match
}
Example of a switch
Case:
Let’s look at an example where we check the day of the week and return a corresponding message.
let day = 'Monday';
switch(day) {
case 'Monday':
console.log("Start of the week!");
break;
case 'Wednesday':
console.log("Mid-week.");
break;
case 'Friday':
console.log("Almost the weekend!");
break;
default:
console.log("It's just another day.");
}