JavaScript switch Statement

In this chapter, we will learn about the JavaScript switch statement. The switch statement allows you to execute one of many blocks of code based on the value of an expression. We will cover:

  • What is the switch Statement?
  • Syntax
  • Using the switch Statement
  • The default Case
  • Multiple Cases
  • Simple Programs using switch Statement

What is the switch Statement?

The switch statement is used to perform different actions based on different conditions. It is an alternative to using multiple if...else if statements.

Syntax

switch (expression) { case value1: // code to be executed if expression === value1 break; case value2: // code to be executed if expression === value2 break; // add more cases as needed default: // code to be executed if expression doesn't match any case } 
  • expression: The expression that is evaluated once and compared with the values of each case.
  • case value: The value to compare the expression against.
  • break: Terminates the switch statement. If omitted, the next case will be executed.
  • default: Optional. Executes if no matching case is found.

Using the switch Statement

Example

let day = 2; switch (day) { case 1: console.log("Monday"); break; case 2: console.log("Tuesday"); break; case 3: console.log("Wednesday"); break; default: console.log("Another day"); } // Output: Tuesday 

In the example above, the expression day is compared with each case value. Since day is 2, the code inside case 2 is executed, and "Tuesday" is printed to the console.

The default Case

The default case is executed if no matching case is found. It is similar to the else statement in an if...else if structure.

Example

let day = 5; switch (day) { case 1: console.log("Monday"); break; case 2: console.log("Tuesday"); break; case 3: console.log("Wednesday"); break; default: console.log("Another day"); } // Output: Another day 

In the example above, since day does not match any of the case values, the default case is executed, and "Another day" is printed to the console.

Multiple Cases

You can group multiple cases together if they should execute the same code.

Example

let fruit = "apple"; switch (fruit) { case "banana": case "apple": case "mango": console.log("This is a fruit."); break; default: console.log("Unknown fruit."); } // Output: This is a fruit. 

In the example above, if fruit is "banana", "apple", or "mango", the same code block will be executed.

Simple Programs using switch Statement

Program 1: Check the Day of the Week

let day = 3; switch (day) { 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 day"); } // Output: Tuesday 

Program 2: Determine the Grade Based on Marks

let marks = 85; let grade; switch (true) { case (marks >= 90): grade = "A"; break; case (marks >= 80): grade = "B"; break; case (marks >= 70): grade = "C"; break; case (marks >= 60): grade = "D"; break; default: grade = "F"; } console.log("Grade:", grade); // Output: Grade: B 

Program 3: Basic Calculator

let a = 10; let b = 5; let operator = "*"; let result; switch (operator) { case "+": result = a + b; break; case "-": result = a - b; break; case "*": result = a * b; break; case "/": result = a / b; break; default: console.log("Invalid operator"); } console.log("Result:", result); // Output: Result: 50 

Conclusion

In this chapter, you learned about the JavaScript switch statement, including its syntax, how to use it, the default case, and handling multiple cases. We also covered some simple programs to demonstrate the usage of the switch statement. The switch statement is used for making decisions based on multiple conditions. In the next chapter, we will explore JavaScript for loop and how to use it to repeat tasks in your programs.

Leave a Comment

Scroll to Top