Lesson 20 of 30 – JavaScript Switch Statement
56%

JavaScript Switch Statement

The switch statement is a conditional statement in JavaScript that allows you to execute one block of code from multiple choices.

Note: Switch statements are useful when multiple conditions depend on the same value.
What is a Switch Statement?

The switch statement evaluates an expression and compares its value with different cases.

  • Acts as an alternative to multiple if-else statements.
  • Makes code cleaner and easier to read.
  • Useful when checking many possible values.
  • Supports numbers, strings, and expressions.
Why Use Switch?

Switch statements help simplify decision-making when there are many possible outcomes.

  • Improves code readability.
  • Reduces lengthy if-else chains.
  • Easy to maintain and modify.
  • Efficient for multiple fixed values.
Switch Statement Syntax
Syntax:
switch(expression){

case value1:
  // code block
  break;

case value2:
  // code block
  break;

default:
  // default code
}

The expression is evaluated once and compared against each case value.

Flow of Execution
  1. JavaScript evaluates the expression.
  2. It compares the result with each case value.
  3. When a matching case is found, that block executes.
  4. The break statement stops execution.
  5. If no case matches, the default block executes.
Simple Switch Example
Example:
var a = 4;

switch(a){
case 1:
document.write("Case 1");
break;

case 2:
document.write("Case 2");
break;

case 3:
document.write("Case 3");
break;

case 4:
document.write("Case 4");
break;

default:
document.write("Default Case");
}
Output:

Case 4

Since the value of a is 4, case 4 is executed.

Understanding the Break Statement

The break statement stops execution after a matching case is found.

Without break, JavaScript continues executing the next cases even if they do not match.

Always use break unless you intentionally want multiple cases to execute.
Switch Statement with Strings

Switch statements can also work with string values.

Example:
var str = "lavu";

switch(str){
case "lavu":
document.write("This is Lavu");
break;

case "sona":
document.write("This is Sona");
break;

case "lovely":
document.write("This is Lovely");
break;

default:
document.write("Unknown Person");
}
Output:

This is Lavu

Combined Switch Cases

Multiple cases can share the same code block.

Example:
var a = 2;

switch(a){
case 1:
case 2:
case 3:
document.write("Case 1, 2 and 3 executed");
break;

case 4:
document.write("Case 4 executed");
break;

default:
document.write("Default Case");
}
Output:

Case 1, 2 and 3 executed

Default Case

The default case executes when none of the cases match the expression.

Example:
var color = "Green";

switch(color){
case "Red":
document.write("Red Color");
break;

case "Blue":
document.write("Blue Color");
break;

default:
document.write("Color Not Found");
}
Output:

Color Not Found

Switch Without Break

If break is omitted, execution continues into the next cases. This is called Fall Through.

Example:
var num = 1;

switch(num){
case 1:
document.write("One
");

case 2:
document.write("Two
");

case 3:
document.write("Three");
}
Output:

One
Two
Three

Switch vs If Else
Switch Statement If Else Statement
Used for fixed values. Used for complex conditions.
Easy to read. Can become lengthy.
Better for many choices. Better for ranges and logical expressions.
Uses case blocks. Uses conditional expressions.
Real-Life Example

A school management system can use switch to display class sections.

Example:
var section = "A";

switch(section){
case "A":
document.write("Class A");
break;

case "B":
document.write("Class B");
break;

default:
document.write("Unknown Section");
}
Output:

Class A

Advantages of Switch Statement
  • Improves code readability.
  • Reduces multiple if-else statements.
  • Easy to maintain.
  • Faster for multiple fixed values.
  • Supports grouping of cases.
  • Makes programs more organized.
Points to Remember
  • The switch statement is an alternative to multiple if-else statements.
  • It compares an expression against multiple case values.
  • The break statement stops execution of the switch block.
  • The default block executes when no case matches.
  • Multiple cases can share the same code block.
  • Switch statements support numbers and strings.
  • Without break, execution continues to the next case.
Key Points
  • Switch is a conditional statement.
  • Case values are compared using strict comparison.
  • Break prevents fall-through behavior.
  • Default acts like the final else block.
  • Switch statements improve readability.
  • Useful when checking many fixed values.
Quick Revision
Keyword Purpose
switch Starts the switch block
case Checks a specific value
break Stops execution
default Executes when no case matches
Best Practices
  • Always use break unless fall-through is required.
  • Use meaningful case values.
  • Keep switch blocks clean and organized.
  • Include a default case whenever possible.
  • Use switch for fixed values, not complex conditions.
  • Indent code properly for readability.

🧠 Quick Quiz

Which keyword is used to stop execution inside a switch statement?