-
Hajipur, Bihar, 844101
Hajipur, Bihar, 844101
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 Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Window
JS Screen
JS Location
JS History
JS Navigator
JS Popup Alert
JS Timing
JS Cookies
Web Storage API
JS Web APIs
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Canvas
JS Graphics & Charts
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 Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Window
JS Screen
JS Location
JS History
JS Navigator
JS Popup Alert
JS Timing
JS Cookies
Web Storage API
JS Web APIs
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Canvas
JS Graphics & Charts
switch
– An Alternative to Multiple if...else
StatementsThe switch
statement is used when you want to compare the same variable or expression against multiple possible values.
switch (expression) {
case value1:
// code block
break;
case value2:
// code block
break;
...
default:
// default code block
}
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.
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
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.
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.
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).
Q1: What is the correct keyword to start a switch block?
Q2: Which keyword prevents the execution of the next case in a switch?
Q3: Which block in a switch is optional but runs if no case matches?
Q4: How many default blocks can you have in a switch?
Q5: Can two case labels point to the same code block?
Q6: Which of the following causes fall-through in a switch block?
Q7: Which data types can be compared in a switch statement?
Q8: What happens if switch matches a case but no break is used?