Member-only story

JavaScript switch case vs object literal

Pravin M
JavaScript in Plain English
5 min readOct 13, 2024
JavaScript switch case vs object literal
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.");
}

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Published in JavaScript in Plain English

New JavaScript and Web Development content every day. Follow to join our 3.5M+ monthly readers.

Responses (1)

What are your thoughts?

against multiple values

Expressions, not values