JavaScript

JS Basics

JS Variables & Operators

JS Data Types & Conversion

JS Numbers & Math

JS Strings

JS Dates

JS Arrays

JS Control Flow

JS Loops & Iteration

JS Functions

JS Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

JavaScript

JS Basics

JS Variables & Operators

JS Data Types & Conversion

JS Numbers & Math

JS Strings

JS Dates

JS Arrays

JS Control Flow

JS Loops & Iteration

JS Functions

JS Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

JS Switch


📘 JavaScript switchAn Alternative to Multiple if...else Statements

The switch statement is used when you want to compare the same variable or expression against multiple possible values.


🔹 Syntax

switch (expression) {
  case value1:
    // code block
    break;
  case value2:
    // code block
    break;
  ...
  default:
    // default code block
}

🔹 How It Works

  • The expression is evaluated once.

  • It is compared with each case value.

  • If a match is found, that block runs.

  • The break statement prevents fall-through (running multiple cases).

  • The default block runs if no matches are found.


🔹 Example 1 – Basic Switch

let day = 3;
let dayName;

switch (day) {
  case 1:
    dayName = "Monday";
    break;
  case 2:
    dayName = "Tuesday";
    break;
  case 3:
    dayName = "Wednesday";
    break;
  default:
    dayName = "Invalid day";
}

console.log(dayName); // Wednesday

🔹 Example 2 – Without break (Fall-through)

let fruit = "apple";

switch (fruit) {
  case "apple":
    console.log("It's an apple");
  case "banana":
    console.log("It's a banana");
  default:
    console.log("Unknown fruit");
}

// Output:
// It's an apple
// It's a banana
// Unknown fruit

🟠 Without break, all matching and following cases will execute.


🔹 Example 3 – Grouped Cases

let score = "B";

switch (score) {
  case "A":
  case "B":
    console.log("Good job!");
    break;
  case "C":
    console.log("You passed.");
    break;
  default:
    console.log("Try again.");
}

✅ Use grouped cases for same outcomes.


Practice Questions

Q1. How do you write a switch statement to check for 3 possible grades (A, B, C)?

Q2. What is the purpose of the break statement inside a switch block?

Q3. How do you use the default case in a switch statement?

Q4. Can multiple case labels execute the same code block in a switch?

Q5. What will happen if you forget to use break in a switch block?

Q6. How do you match a variable against 5 possible values using switch?

Q7. Can you use expressions like x > 5 inside a case label?

Q8. How do you check for both "Saturday" and "Sunday" as weekends in one case block?

Q9. What does JavaScript do if none of the case values match and there is no default block?

Q10. Write a switch that prints the name of a month based on its number (1–12).


JS Switch Quiz

Q1: What is the correct keyword to start a switch block?

A. switch()
B. case
C. if
D. switch

Q2: Which keyword prevents the execution of the next case in a switch?

A. continue
B. stop
C. break
D. exit

Q3: Which block in a switch is optional but runs if no case matches?

A. else
B. otherwise
C. default
D. fallback

Q4: How many default blocks can you have in a switch?

A. 0
B. 1
C. Multiple
D. Depends on the code

Q5: Can two case labels point to the same code block?

A. No
B. Only if they are strings
C. Yes
D. Only with numbers

Q6: Which of the following causes fall-through in a switch block?

A. Using continue
B. Omitting break
C. Adding default
D. Using if inside case

Q7: Which data types can be compared in a switch statement?

A. Only numbers
B. Strings and numbers
C. Arrays
D. Objects

Q8: What happens if switch matches a case but no break is used?

A. Only the matched case runs
B. All following cases run
C. Error occurs
D. Default runs only

Go Back Top