In JavaScript, the switch statement is used for making decisions based on the value of an expression. It provides a way to compare a single value against multiple possible case values and execute code blocks based on which case value matches the value of the expression. Here's the basic syntax of the switch statement:
switch (expression) {
case value1:
// Code to execute if expression matches value1
break;
case value2:
// Code to execute if expression matches value2
break;
// Additional cases can be added as needed.
default:
// Code to execute if none of the cases match the expression
}Here's a breakdown of how it works:
-
The
switchkeyword is followed by an expression enclosed in parentheses. This expression is the value that you want to compare against the different case values. -
Inside the
switchblock, you define one or morecaseblocks. Eachcaseblock specifies a value that you want to compare with the expression. If the expression matches acasevalue, the code inside thatcaseblock will be executed. -
After each
caseblock, you typically include abreakstatement. Thisbreakstatement is used to exit theswitchstatement after executing the code in the matchingcase. Without abreakstatement, theswitchstatement would continue to execute the code in subsequentcaseblocks. -
You can also include a
defaultblock at the end of theswitchstatement. Thedefaultblock is executed if none of thecasevalues match the expression.
Here's an example:
let day = "Monday";
switch (day) {
case "Monday":
console.log("It's the start of the workweek.");
break;
case "Friday":
console.log("It's almost the weekend.");
break;
default:
console.log("It's a regular day.");
}In this example, the switch statement compares the value of the day variable against different cases and prints a message based on the value of day. Since day is "Monday" in this case, it will print "It's the start of the workweek."
The switch statement is useful when you have multiple conditions to check against a single expression, and it can provide a cleaner and more efficient way to handle these situations compared to using a series of if/else if statements.
const number = parseInt(prompt('Enter a number between 1 to 7: '));
switch (number) {
case 1:
console.log("Sunday");
break;
case 2:
console.log("Monday");
break;
case 3:
console.log("Tuesday");
break;
case 4:
console.log("Wednesday");
break;
case 5:
console.log("Thursday");
break;
case 6:
console.log("Friday");
break;
case 7:
console.log("Saturday");
break;
default:
console.log("Invalid Number");
}Output
Enter a number between 1 to 7: 4
Wednesday
const number = parseInt(prompt('Enter a number between 1 to 7: '));
switch (number) {
case 1:
console.log("Sunday");
case 2:
console.log("Monday");
case 3:
console.log("Tuesday");
case 4:
console.log("Wednesday");
case 5:
console.log("Thursday");
case 6:
console.log("Friday");
case 7:
console.log("Saturday");
default:
console.log("Invalid Number");
}Output
Wednesday
Thursday
Friday
Saturday
Invalid Number
const day = prompt('Enter the day: ');
switch (day) {
case 'Monday':
case 'Tuesday':
case 'Wednesday':
case 'Thursday':;
case 'Friday':
console.log("Weekday");
break;
case 'Saturday':
case 'Sunday':
console.log("Weekend");
break;
default:
console.log("Invalid Day");
}Output
Weekday
// take the operator input
const operator = prompt('Enter operator ( either +, -, * or / ): ');
// take the operand input
const number1 = parseFloat(prompt('Enter first number: '));
const number2 = parseFloat(prompt('Enter second number: '));
let result;
switch(operator) {
case '+':
result = number1 + number2;
break;
case '-':
result = number1 - number2;
break;
case '*':
result = number1 * number2;
break;
case '/':
result = number1 / number2;
break;
default:
console.log('Invalid operator');
break;
}
console.log(`${number1} ${operator} ${number2} = ${result}`);Output
Enter operator: *
Enter first number: 5
Enter second number: 8.8
5 * 8.8 = 44
Can you use switch statements to create a program that takes the number input from the user from 1 to 12. And, print the corresponding month based on the input value.
const number = parseInt(prompt('Enter a number from 1 to 12: '));
let result;
switch(number) {
case 1:
result = "January";
break;
case 2:
result = "Febraury";
break;
case 3:
result = "March";
break;
case 4:
result = "April";
break;
case 5:
result = "May";
break;
case 6:
result = "June";
break;
case 7:
result = "July";
break;
case 8:
result = "August";
break;
case 9:
result = "September";
break;
case 10:
result = "October";
break;
case 11:
result = "November";
break;
case 12:
result = "December";
break;
default:
console.log('Invalid number');
break;
}
console.log(result);Output
Enter a number from 1 to 12: 3
March
Q. Which of the cases is executed in the following code?
let value = 4;
switch(value) {
case 1:
case 2:
default:
}- case 1
- case 2
- case 4
- default
Answer: 4