🟡 Control Flow · Lesson 15
switch Statement
switch Statement
The switch Statement
let day = 3;
switch (day) {
case 1:
console.log("Monday");
break;
case 2:
console.log("Tuesday");
break;
case 3:
console.log("Wednesday"); // this runs
break;
default:
console.log("Invalid day");
}
switch compares ONE value against several possible cases — a cleaner alternative to a long else if chain when you're checking the same variable over and over. You write switch (value), then list each possibility as a case. JavaScript finds the case that matches and runs the code beneath it. Note that switch uses STRICT comparison (===) internally, so case "3" would NOT match the number 3. It shines when checking things like menu choices, day numbers, status codes, or command names.Why break Matters — the fall-through trap
// WITHOUT break — everything after the match also runs!
switch (2) {
case 1: console.log("One");
case 2: console.log("Two"); // matches, runs...
case 3: console.log("Three"); // ...and this runs too! (fall-through)
}
// Output: Two, Three ← probably NOT what you wanted
break stops the switch once a case has run — and forgetting it is the classic switch bug. Without break, execution "falls through" into every case BELOW the match, running all of them regardless of their values. This surprises everyone the first time. So the rule is: end every case with break (the last case doesn't strictly need it, but adding it is safer if you later add more cases). Fall-through is occasionally useful on purpose, which you'll see next — but when it's accidental, it causes very confusing bugs.The default Case — the catch-all
let grade = "X";
switch (grade) {
case "A": console.log("Excellent"); break;
case "B": console.log("Good"); break;
default:
console.log("Invalid grade"); // runs when nothing matched
}
default is the switch equivalent of else — it runs when none of the cases matched. It's optional, but including it is good practice: it handles unexpected values gracefully instead of silently doing nothing. Conventionally, default is written last (though technically it can appear anywhere). Use it to show an error message, set a fallback value, or log an unexpected input. Just like a final else, it's your safety net.Grouping Cases — deliberate fall-through
let day = "Sunday";
switch (day) {
case "Saturday":
case "Sunday":
console.log("Weekend!"); // runs for BOTH cases
break;
default:
console.log("Weekday");
}
You can stack multiple
case labels together to run the same code for several values. Because a case without a break falls through to the next one, writing case "Saturday": immediately above case "Sunday": means both lead to the same block. This is fall-through used ON PURPOSE, and it's a genuinely useful pattern — grouping months into seasons, grades into pass/fail, or days into weekend/weekday. It keeps your code short and avoids repeating identical blocks.switch vs if...else — which to use
| Use switch when | Use if/else when |
|---|---|
| Comparing ONE variable to many fixed values | Conditions are ranges (marks >= 75) |
| Values are exact (numbers, strings) | Conditions involve &&, ||, or comparisons |
| Many cases (menu options, commands) | Only two or three branches |
The dividing line is simple:
switch tests one value for EQUALITY against fixed options; if/else handles anything more complex. You couldn't write the grade example from the last chapter (marks >= 90) as a switch, because switch can't do ranges or comparisons — only exact matches. But for a menu with eight options, switch is far more readable than eight else ifs. Choose whichever makes the intent clearest; both do the job, and neither is faster in any way you'd notice.Exam Corner
Q: What does the switch statement do? Compares one value against multiple cases and runs the matching one.
Q: What happens if you forget break? Execution falls through and runs the cases below the match too.
Q: What is default in a switch? The block that runs when no case matches — like else.
Q: How do you run the same code for two values? Stack the case labels together with no break between them.
Q: When should you use if/else instead of switch? When conditions involve ranges, comparisons, or && / ||.
Q: What happens if you forget break? Execution falls through and runs the cases below the match too.
Q: What is default in a switch? The block that runs when no case matches — like else.
Q: How do you run the same code for two values? Stack the case labels together with no break between them.
Q: When should you use if/else instead of switch? When conditions involve ranges, comparisons, or && / ||.
switch Statement
let day = 3;
switch (day) {
case 1:
console.log("Monday");
break;
case 2:
console.log("Tuesday");
break;
case 3:
console.log("Wednesday"); // yeh chalta hai
break;
default:
console.log("Invalid day");
}
switch EK value की तुलना कई संभव cases से करता है — लंबी else if chain का साफ विकल्प जब आप बार-बार वही variable जांच रहे हों. आप switch (value) लिखते हैं, फिर हर संभावना case के रूप में सूचीबद्ध करते हैं. JavaScript मिलने वाला case ढूंढकर उसके नीचे का code चलाता है. ध्यान दीजिए switch अंदर से STRICT तुलना (===) use करता है, तो case "3" number 3 से MATCH नहीं करेगा. यह menu choices, day numbers, status codes, या command names जैसी चीज़ें जांचते समय चमकता है.break क्यों ज़रूरी — fall-through जाल
// break KE BINA — match ke baad ka sab bhi chalta hai!
switch (2) {
case 1: console.log("One");
case 2: console.log("Two"); // match, chalta...
case 3: console.log("Three"); // ...aur yeh bhi chalta! (fall-through)
}
// Output: Two, Three ← shayad NAHI jo aap chahte the
break case चलने के बाद switch रोकता है — और इसे भूलना classic switch bug है. break के बिना, execution match के NEECHE के हर case में "गिरता" है, उन सबको चलाते चाहे उनकी values कुछ भी हों. यह पहली बार सबको चौंकाता है. तो rule है: हर case को break से खत्म कीजिए (आखिरी case को सख्ती से ज़रूरत नहीं, पर जोड़ना सुरक्षित है अगर बाद में और cases जोड़ें). Fall-through कभी-कभी जानबूझकर उपयोगी है, जो आप आगे देखेंगे — पर जब यह आकस्मिक हो, बहुत उलझाने वाले bugs पैदा करता है.default Case — catch-all
let grade = "X";
switch (grade) {
case "A": console.log("Excellent"); break;
case "B": console.log("Good"); break;
default:
console.log("Invalid grade"); // jab kuch match na ho
}
default switch में else के बराबर है — यह तब चलता है जब कोई case match न हो. यह optional है, पर शामिल करना अच्छी practice है: यह अनपेक्षित values को चुपचाप कुछ न करने के बजाय शालीनता से संभालता है. परंपरागत रूप से default आखिर में लिखा जाता है (हालांकि तकनीकी रूप से कहीं भी आ सकता है). इसे error message दिखाने, fallback value set करने, या अनपेक्षित input log करने को use कीजिए. आखिरी else की तरह, यह आपका safety net है.Cases Group करना — जानबूझकर fall-through
let day = "Sunday";
switch (day) {
case "Saturday":
case "Sunday":
console.log("Weekend!"); // DONO cases ke liye chalta
break;
default:
console.log("Weekday");
}
आप कई
case labels एक साथ रखकर कई values के लिए वही code चला सकते हैं. क्योंकि break बिना case अगले में गिरता है, case "Saturday": को case "Sunday": के ठीक ऊपर लिखना मतलब दोनों उसी block पर ले जाते हैं. यह JAANBUJHKAR इस्तेमाल किया fall-through है, और सच में उपयोगी pattern — महीनों को मौसमों में, grades को pass/fail में, या दिनों को weekend/weekday में group करना. यह आपका code छोटा रखता है और एक जैसे blocks दोहराने से बचाता है.switch vs if...else — कौन-सा use करें
| switch तब जब | if/else तब जब |
|---|---|
| EK variable की कई fixed values से तुलना | Conditions ranges हों (marks >= 75) |
| Values exact हों (numbers, strings) | Conditions में &&, ||, या comparisons हों |
| कई cases (menu options, commands) | सिर्फ दो-तीन branches |
विभाजन रेखा सरल है:
switch एक value की fixed options से BARABARI जांचता है; if/else इससे जटिल कुछ भी संभालता है. आप पिछले chapter का grade example (marks >= 90) switch के रूप में नहीं लिख सकते, क्योंकि switch ranges या comparisons नहीं कर सकता — सिर्फ exact matches. पर आठ options वाले menu के लिए, switch आठ else if से कहीं ज़्यादा readable है. जो भी इरादा साफ करे वही चुनिए; दोनों काम करते हैं, और किसी में ऐसी तेज़ी नहीं जो आप महसूस करें.Exam Corner
Q: switch statement क्या करता है? एक value की कई cases से तुलना करके मिलने वाला चलाता है.
Q: break भूल जाएं तो क्या होता है? Execution गिरता है और match के नीचे के cases भी चलाता है.
Q: switch में default क्या है? वह block जो तब चलता है जब कोई case match न हो — else जैसा.
Q: दो values के लिए वही code कैसे चलाते हैं? Case labels को बिना break के एक साथ रखिए.
Q: switch के बजाय if/else कब use करें? जब conditions में ranges, comparisons, या && / || हों.
Q: break भूल जाएं तो क्या होता है? Execution गिरता है और match के नीचे के cases भी चलाता है.
Q: switch में default क्या है? वह block जो तब चलता है जब कोई case match न हो — else जैसा.
Q: दो values के लिए वही code कैसे चलाते हैं? Case labels को बिना break के एक साथ रखिए.
Q: switch के बजाय if/else कब use करें? जब conditions में ranges, comparisons, या && / || हों.
💻 Live Code Editor
इस पेज का code यहाँ तैयार है — बदलिए और तुरंत नतीजा देखिए. कुछ install किए बिना.
सब कुछ आपके browser में ही चलता है — कोई server, कोई signup नहीं. JavaScript का
console.log देखने के लिए F12 दबाइए.