JS स्विच
switch (expression) {
case value1:
// code
break;
default:
// code
}
Basic switch Syntax
एक switch statement parentheses में एक value लेता है और उसे क्रम में हर case label से compare करता है, और पहली मैच होने वाली case block के अंदर का code चलाता है।
उदाहरण: Basic switch Syntax
// Declare the constant `day`, set to "Mon"
// Declare the constant `day`, set to "Mon"
const day = "Mon";
// Switch on `day` and branch by matching case
// Switch on `day` and branch by matching case
switch (day) {
// Case: `"Mon"`
// Case: `"Mon"`
case "Mon":
// Print "Start of week" to the console
// Print "Start of week" to the console
console.log("Start of week");
}
case और break
हर case label किसी खास matching value के लिए चलने वाले block की शुरुआत बताता है, और break उसका block खत्म होते ही switch statement से तुरंत बाहर निकल जाता है, जिससे execution अगले case में जारी नहीं रहता।
उदाहरण: case and break
// Declare the constant `day`, set to "Mon"
// Declare the constant `day`, set to "Mon"
const day = "Mon";
// Switch on `day` and branch by matching case
// Switch on `day` and branch by matching case
switch (day) {
// Case: `"Mon"`
// Case: `"Mon"`
case "Mon":
// Print "Monday" to the console
// Print "Monday" to the console
console.log("Monday");
break;
// Case: `"Tue"`
// Case: `"Tue"`
case "Tue":
// Print "Tuesday" to the console
// Print "Tuesday" to the console
console.log("Tuesday");
break;
}
default
default case तब चलता है जब लिस्ट में से कोई भी case label switch value से मैच नहीं करता, यह if/else if chain में आखिरी else की तरह काम करता है, और आमतौर पर सबसे आखिर में रखा जाता है, हालांकि technically यह कहीं भी आ सकता है।
उदाहरण: default
// Declare the constant `day`, set to "Sun"
// Declare the constant `day`, set to "Sun"
const day = "Sun";
// Switch on `day` and branch by matching case
// Switch on `day` and branch by matching case
switch (day) {
// Case: `"Mon"`
// Case: `"Mon"`
case "Mon":
// Print "Weekday" to the console
// Print "Weekday" to the console
console.log("Weekday");
break;
// Default case, if nothing above matched
// Default case, if nothing above matched
default:
// Print "Not Monday" to the console
// Print "Not Monday" to the console
console.log("Not Monday");
}
Cases को साथ Group करना
कई case labels एक ही block of code को साझा कर सकते हैं अगर उन्हें बीच में break के बिना stack किया जाए, जिससे कई अलग-अलग values एक ही response trigger कर सकें।
उदाहरण: Grouping Cases Together
// Declare the constant `day`, set to "Sat"
// Declare the constant `day`, set to "Sat"
const day = "Sat";
// Switch on `day` and branch by matching case
// Switch on `day` and branch by matching case
switch (day) {
// Case: `"Sat"`
// Case: `"Sat"`
case "Sat":
// Case: `"Sun"`
// Case: `"Sun"`
case "Sun":
// Print "Weekend" to the console
// Print "Weekend" to the console
console.log("Weekend");
break;
// Default case, if nothing above matched
// Default case, if nothing above matched
default:
// Print "Weekday" to the console
// Print "Weekday" to the console
console.log("Weekday");
}
switch बनाम if-else
जब एक single value को कई specific possibilities से compare करना हो तो switch आमतौर पर ज़्यादा साफ होता है, जबकि ranges, कई variables, या logical combinations वाली जटिल conditions के लिए if-else chains ज़्यादा flexible हैं।
उदाहरण: switch vs if-else
const num = 5;
// switch is clean for one value vs many options:
switch (num) {
case 5:
console.log("Five");
break;
}
// if-else is better for ranges:
if (num > 0 && num < 10) {
console.log("Single digit");
}
- किसी case के अंत में break statement भूल जाना, जिससे execution गलती से अगले case में fall through हो जाता है।
- यह उम्मीद करना कि switch == जैसी loose equality इस्तेमाल करता है, यह असल में cases match करते समय ===, जैसी strict comparison इस्तेमाल करता है।
- default case छोड़ देना, जो listed cases में से कोई भी match न होने पर चुपचाप कुछ नहीं करता।
- switch एक value को case labels की एक series के against compare करता है और matching block चलाता है।
- break किसी match मिलने के बाद execution को अगले case में fall through होने से रोकता है।
- default एक fallback block देता है जो कोई case match न होने पर चलता है, if chain में else जैसा।
switch statement एक core JavaScript feature है जो हर browser और JavaScript environment में समान रूप से supported है।
Chapter Quiz — Complete all 26 topics to unlock
0/26 topics done
Complete these topics first:
- JS Dates
- JS Math
- JS Conditionals
- JS Switch
- JS Loop For
- JS Loop While
- JS Iterables
- JS Sets
- JS Maps
- JS typeof
- JS Type Conversion
- JS Destructuring
- JS Arrow Functions
- JS Classes
- JS Modules
- JS Promises
- JS Async/Await
- JS DOM
- JS DOM Methods
- JS Events Advanced
- JS DOM Navigation
- JS DOM Collections
- JS Async Callbacks
- JS Async Parallel
- JS Date Set
- JS Set Logic