ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
Vending machine တစ်လုံးကို စဉ်းစားကြည့်ပါ။ ခလုတ်နံပါတ် 1 ကို နှိပ်ရင် ရေ၊ 2 ကို နှိပ်ရင် ကော်ဖီ၊ မရှိတဲ့ နံပါတ်ကို နှိပ်ရင် "ရွေးချယ်မှု မမှန်ပါ" လို့ ပြပါတယ်။ စက်က နံပါတ်တစ်ခုကို ယူပြီး ကြိုတင်သတ်မှတ်ထားတဲ့ ရွေးချယ်စရာ စာရင်းနဲ့ တိုက်ကြည့်ပြီး ကိုက်တဲ့ တစ်ခုကို လုပ်ပေးတာပါ။
JavaScript ရဲ့ switch statement က အဲဒီ စက်အတိုင်းပါပဲ။ switch (value) ထဲမှာ value တစ်ခု ထည့်ပေးပြီး case "water": လို label တွေက ရွေးချယ်စရာတွေပါ။ ဘယ် case နဲ့မှ မကိုက်ရင် default: branch က run ပါတယ်။ if...else if တွေ အရှည်ကြီး ဆက်ရေးနေရမယ့်အစား value တစ်ခုတည်းကို စစ်တဲ့ အခါမှာ ပိုဖတ်ရလွယ်ပါတယ်။
Problem → Why → Solution
ပြဿနာ: break တစ်ခု ကျန်ခဲ့ရင် ပို့ခ မှားသွားတယ်
Online shop တစ်ခုမှာ ပို့ဆောင်ခကို zone အလိုက် တွက်တဲ့ function ကို ရေးထားတယ်ဆိုပါစို့။ "local" zone အတွက် 2 ဒေါ်လာ ဖြစ်ရမှာပါ။ ဒါပေမယ့် customer တွေ ပြည်တွင်း (national) ဈေးနှုန်း 5 ဒေါ်လာ အပေးခံနေရပါတယ်။ Error message တစ်ခုမှ မပေါ်ပါဘူး၊ code ကလည်း မှန်နေသလို ထင်ရပါတယ်။
function shippingFee(zone) {
let fee;
switch (zone) {
case "local":
fee = 2; // no break here!
case "national":
fee = 5;
break;
default:
fee = 15;
}
return fee;
}
console.log(shippingFee("local")); // 5, not 2ဘာကြောင့် 5 ထွက်လာတာလဲ
switch က ကိုက်တဲ့ case ကို ရှာတွေ့ရင် အဲဒီနေရာကို ခုန်သွားပြီး break, return ဒါမှမဟုတ် switch block အဆုံး မရောက်မချင်း အောက်က statement တွေကို ဆက်တိုက် run ပါတယ်။ နောက် case label တွေက တံခါးတွေ မဟုတ်ဘဲ နေရာပြ မှတ်တိုင်တွေပဲ ဖြစ်ပါတယ်။ ဒါကြောင့် fee = 2 ပြီးတဲ့နောက် fee = 5 ကိုပါ ဆက် run ပြီး တန်ဖိုးကို ထပ်ရေးသွားပါတယ်။ ဒါကို fallthrough လို့ ခေါ်ပါတယ်။
Core concept: switch ဘယ်လို အလုပ်လုပ်လဲ
switch (expression) ကို run တဲ့အခါ JavaScript က ကွင်းထဲက expression ကို တစ်ကြိမ်ပဲ တွက်ပါတယ်။ ပြီးရင် case တွေကို အပေါ်ကနေ အောက်ကို အစဉ်လိုက် strict equality (===) နဲ့ နှိုင်းယှဉ်ပါတယ်။ ဆိုလိုတာက type ပြောင်းပေးတာ မရှိပါဘူး — switch ("1") က case 1: နဲ့ မကိုက်ပါဘူး။ Form input, URL query, prompt() ကနေ ရလာတဲ့ data တွေက အမြဲ string ဖြစ်လို့ ဒါက တကယ် ကြုံရတဲ့ bug ပါ။
ကိုက်တဲ့ case ကို တွေ့တာနဲ့ အဲဒီ label ရဲ့ အောက်ကို ခုန်ပြီး statement တွေကို run ပါတယ်။ break က switch ထဲကနေ ချက်ချင်း ထွက်စေပြီး function ထဲမှာဆိုရင် return ကလည်း အတူတူ ထွက်စေပါတယ်။ နှစ်ခုလုံး မပါရင် နောက် case ရဲ့ code ထဲ ဆက်ဝင်သွားပါတယ်။ ဒီ fallthrough ကို ရည်ရွယ်ချက်ရှိရှိ သုံးတာက case တွေကို စုပေါင်းတာပါ — case "sat": case "sun": လို label နှစ်ခုကို ကပ်ရေးလိုက်ရင် code တစ်ခုတည်းကို မျှသုံးပါတယ်။
default က ဘယ် case နဲ့မှ မကိုက်တဲ့အခါ run ပါတယ်။ Syntax အရ ဘယ်နေရာမှာ ထားထား ရပေမယ့် အဆုံးမှာ ထားတာ ဓလေ့ပါ။ default မပါရင် မသိတဲ့ value တစ်ခု ဝင်လာတဲ့အခါ ဘာမှ မဖြစ်ဘဲ တိတ်တိတ်လေး ကျော်သွားပါတယ်၊ ဒါကြောင့် default ထဲမှာ fallback value ပြန်တာ ဒါမှမဟုတ် error throw လုပ်တာနဲ့ policy ကို ရှင်းရှင်းလင်းလင်း ရေးထားပါ။
switch က value တစ်ခုကို ကန့်သတ်ထားတဲ့ ရွေးချယ်စရာ အစုတစ်ခု (status, command, weekday နံပါတ်) နဲ့ တိုက်တဲ့အခါ အကောင်းဆုံးပါ။ score >= 90 လို အပိုင်းအခြား (range) စစ်ချင်ရင် if...else က ပိုသင့်တော်ပါတယ်။ Value တစ်ခုကို တခြား value တစ်ခုဆီ map လုပ်ရုံပဲ ဆိုရင်တော့ lookup object က ပိုတိုပါတယ်။
case value တွေက literal ဖြစ်စရာ မလိုပါဘူး။ case MAX_LEVEL: ဒါမှမဟုတ် case "ad" + "min": လို constant နဲ့ expression တွေလည်း ရပါတယ်၊ ကိုက်တဲ့ တစ်ခု မတွေ့မချင်း case expression တစ်ခုချင်းကို အစဉ်လိုက် တွက်သွားလို့ပါ။ ဒါပေမယ့် ရွေးချယ်စရာ စာရင်းကို တစ်ချက်ကြည့်ရုံနဲ့ နားလည်အောင် code အများစုက literal နဲ့ constant တွေကိုပဲ သုံးကြပါတယ်။
case တွေက block တစ်ခုတည်းကို မျှသုံးတယ်
switch တစ်ခုလုံးက { } block တစ်ခုတည်းပါ။ case နှစ်ခုမှာ let message ကို ကြေညာရင် "Identifier 'message' has already been declared" SyntaxError တက်ပါတယ်။ case တစ်ခုချင်းကို case 1: { ... } လို ကွင်းခတ်ပေးရင် scope ခွဲသွားပါတယ်။
- Expression ကို တစ်ကြိမ် တွက်
- case တွေကို === နဲ့ အစဉ်လိုက် စစ်
- ကိုက်တဲ့ label ဆီ ခုန်
- break / return အထိ အောက်ကို run
case label တွေက နံရံ မဟုတ်ပါ — break ကသာ ရပ်စေတာပါ။
အထွေထွေ ပုံစံ
switch (expression) {
case valueA:
// runs when expression === valueA
break;
case valueB:
case valueC:
// shared code for valueB and valueC
break;
default:
// runs when nothing matched
}case တစ်ခုစီရဲ့ အဆုံးမှာ break ထည့်ပါ။ label နှစ်ခု ကပ်ရေးထားရင် code တစ်ခုတည်းကို မျှသုံးပါတယ်။ default က နောက်ဆုံးမှာ ရှိပြီး ဘာမှ မကိုက်တဲ့အခါ run ပါတယ်။
Example: weekday နံပါတ်ကို နာမည်ပြောင်းခြင်း
new Date().getDay() က Sunday အတွက် 0၊ Monday အတွက် 1 ... Saturday အတွက် 6 ပြန်ပါတယ်။ ဒီနေ့ရဲ့ နေ့ကို သုံးရင် output က run တဲ့နေ့ပေါ် မူတည်လို့ ဒီ example မှာ value တွေကို ကိုယ်တိုင် ပို့ပြီး switch ကို function ထဲ ထည့်ထားပါတယ်။ နောက်ဆုံး call မှာ string "1" ကို ပို့ထားတာကို သတိထားကြည့်ပါ။
function describeDay(day) {
let dayName;
switch (day) {
case 0:
dayName = "Sunday";
break;
case 1:
dayName = "Monday";
break;
case 6:
dayName = "Saturday";
break;
default:
dayName = "Another day";
}
return dayName;
}
// describeDay(new Date().getDay()) would use today; fixed values keep output stable
console.log("0 -> " + describeDay(0));
console.log("1 -> " + describeDay(1));
console.log("3 -> " + describeDay(3));
console.log('"1" -> ' + describeDay("1"));Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Line 3
switch (day) {day ကို တစ်ကြိမ်ပဲ ဖတ်ပြီး အောက်က case တွေနဲ့ === နဲ့ အစဉ်လိုက် နှိုင်းယှဉ်ပါတယ်။
- Lines 4-6
case 0:day က 0 ဖြစ်ရင် ဒီကို ခုန်လာပြီး dayName ကို "Sunday" ထားကာ break နဲ့ switch ထဲက ထွက်ပါတယ်။ break မပါရင် Monday ရဲ့ code ကိုပါ ဆက် run မှာပါ။
- Lines 13-14
default:0, 1, 6 မဟုတ်တဲ့ value အားလုံး ဒီကို ရောက်ပါတယ်။ နောက်ဆုံး branch ဖြစ်လို့ break မလိုပါဘူး။
- Line 16
return dayName;switch ရွေးပေးလိုက်တဲ့ နာမည်ကို caller ဆီ ပြန်ပို့ပါတယ်။
- Line 23
console.log('"1" -> ' + describeDay("1"));"1" က string ဖြစ်ပြီး case 1 က number ဖြစ်လို့ === က false ပါ။ ဒါကြောင့် Monday မဟုတ်ဘဲ "Another day" ထွက်ပါတယ်။
0 -> Sunday
1 -> Monday
3 -> Another day
"1" -> Another day- switch (day) ကို တွက်
- case 0, 1, 6 ကို === နဲ့ စစ်
- ကိုက်ရင် အဲဒီ code ကို run ပြီး break
- မကိုက်ရင် default ကို run
ကိုက်တဲ့ case ရှိ
dayName = "Monday"
ဘာမှ မကိုက်
dayName = "Another day"
Case grouping နဲ့ lookup object
Value အများအပြားက ရလဒ်တစ်ခုတည်း ပြန်ရမယ်ဆိုရင် case label တွေကို ကပ်ရေးပြီး fallthrough ကို ရည်ရွယ်ချက်ရှိရှိ သုံးပါ။ Function ထဲမှာ return ကို သုံးရင် break မလိုတော့ပါဘူး၊ return က switch ကိုရော function ကိုပါ ထွက်စေလို့ပါ။ Status တစ်ခုကို စာသားတစ်ခုဆီ map လုပ်ရုံပဲဆိုရင်တော့ object တစ်ခုထဲမှာ key-value အဖြစ် ထားပြီး ?? နဲ့ fallback ပေးတာက switch ထက် တိုပါတယ်။
function dayType(day) {
switch (day) {
case "sat":
case "sun":
return "weekend";
case "mon":
case "tue":
case "wed":
case "thu":
case "fri":
return "weekday";
default:
return "unknown";
}
}
const statusLabels = { pending: "Waiting for payment", paid: "Preparing" };
console.log(dayType("sun"), dayType("wed"), dayType("holiday"));
console.log(statusLabels["paid"] ?? "Unknown status");
console.log(statusLabels["lost"] ?? "Unknown status");Lookup object က data သက်သက်ပဲ ဖြစ်လို့ JSON file ကနေ load လုပ်တာ၊ test ထဲမှာ အစားထိုးတာ လွယ်ပါတယ်။ Branch တစ်ခုချင်းမှာ statement အများကြီး run ရမယ်ဆိုရင်တော့ switch က ပိုရှင်းပါတယ်၊ object ထဲမှာ function တွေ ထည့်ရေးရင် ဖတ်ရခက်လာလို့ပါ။
လက်တွေ့ project တွေမှာ
Real project တွေမှာ ဘယ်လို သုံးလဲ
E-commerce app တွေမှာ order တစ်ခုရဲ့ status (pending, paid, shipped, cancelled) ပေါ် မူတည်ပြီး လုပ်ရမယ့် အလုပ် ကွဲပါတယ် — payment reminder ပို့တာ၊ ပစ္စည်း ထုပ်ပိုးတာ၊ tracking link ပို့တာ၊ refund ပြန်ပေးတာ စသဖြင့်ပါ။ ဒီလို status အစု ကန့်သတ်ထားတဲ့ နေရာမှာ switch က status တိုင်းအတွက် branch တစ်ခုစီကို ရှင်းရှင်းလင်းလင်း ပြပေးပါတယ်။ Database ထဲမှာ status အသစ် "refunded" ထပ်ထည့်လိုက်ရင် default က အဲဒါကို တိတ်တိတ်လေး မကျော်ဘဲ log ထုတ်တာ ဒါမှမဟုတ် error ပြတာ လုပ်သင့်ပါတယ်။
Command-line tool တွေက user ရိုက်ထည့်တဲ့ command (start, stop, status) ကို switch နဲ့ ခွဲပါတယ်။ Redux လို state management library တွေရဲ့ reducer function တွေကလည်း action.type ပေါ် switch လုပ်တာ အသုံးများတဲ့ ပုံစံ ဖြစ်ပါတယ်။ Keyboard shortcut ကိုင်တွယ်တဲ့ code မှာလည်း event.key ("ArrowUp", "Escape") ပေါ် switch သုံးတာ တွေ့ရပါတယ်။ ဒီနေရာ အားလုံးမှာ စစ်ချင်တဲ့ value က တစ်ခုတည်းဖြစ်ပြီး ရွေးချယ်စရာတွေက ကြိုသိထားပြီးသား ဖြစ်ပါတယ်။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. break မေ့တာ
Problem section ထဲက shippingFee လိုပဲ break တစ်ခု ကျန်ခဲ့ရင် နောက် case ရဲ့ code ပါ run ပြီး ရလဒ်ကို ထပ်ရေးသွားပါတယ်။ Error မပေါ်လို့ ရှာရခက်ပါတယ်။ case တိုင်းကို break ဒါမှမဟုတ် return နဲ့ အဆုံးသတ်ပြီး ရည်ရွယ်ချက်ရှိရှိ fallthrough လုပ်တဲ့ နေရာမှာ // falls through comment ရေးထားပါ။ ESLint ရဲ့ no-fallthrough rule ကလည်း ဒါကို ဖမ်းပေးပါတယ်။
2. String နဲ့ number ရောနေတာ
Form field ကနေ ရလာတဲ့ "2" ကို switch ထဲ ထည့်ပြီး case 2: နဲ့ စစ်ရင် === ကြောင့် ဘယ်တော့မှ မကိုက်ဘဲ default ကိုပဲ ရောက်ပါတယ်။ switch (Number(input)) လို ကြိုပြောင်းထားတာ ဒါမှမဟုတ် case "2": လို string label တွေ သုံးတာ တစ်ခုခု ရွေးပါ။
3. Range တွေကို case နဲ့ စစ်တာ
switch (score) { case score >= 90: ... } ဆိုရင် case value က true ဒါမှမဟုတ် false ဖြစ်ပြီး score (number) နဲ့ === နဲ့ နှိုင်းယှဉ်တာမို့ ဘယ်တော့မှ မကိုက်ပါဘူး။ Range တွေအတွက် if...else ကို သုံးပါ။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- default ကို အမြဲ ထည့်ပြီး မသိတဲ့ value အတွက် ဘာလုပ်မလဲ ရှင်းရှင်း ရေးပါ။
- Function ထဲမှာ switch သုံးရင် break အစား return ကို သုံးတာက ပိုတိုပြီး fallthrough အန္တရာယ် လျော့ပါတယ်။
- Value တစ်ခုကို စာသားတစ်ခုဆီ map လုပ်ရုံဆိုရင် lookup object ကို ရွေးပါ။
Mini challenge
Mini challenge: order status handler
Order status တစ်ခုကို လက်ခံပြီး shop ဝန်ထမ်းက လုပ်ရမယ့် အလုပ်ကို စာသားအဖြစ် ပြန်ပေးတဲ့ getOrderAction() function ကို switch နဲ့ ရေးပါ။
- Status ကို trim() နဲ့ toLowerCase() လုပ်ပြီးမှ နှိုင်းယှဉ်ရမယ်
- "pending" → "Send payment reminder"၊ "paid" → "Pack the items"၊ "cancelled" → "Refund the customer"
- "shipped" နဲ့ "out-for-delivery" က case grouping နဲ့ "Share tracking link" ကို မျှသုံးရမယ်
- မသိတဲ့ status ဆိုရင် default က "Unknown status: " နဲ့ မူရင်း value ကို ပြန်ရမယ်
function getOrderAction(status) {
const normalized = ""; // TODO: trim and lower-case status
switch (normalized) {
// TODO: pending, paid and cancelled each return their own action
// TODO: shipped and out-for-delivery share one return
// TODO: default returns "Unknown status: " + the original status
}
}
const samples = ["paid", " SHIPPED ", "out-for-delivery", "cancelled", "lost"];
// TODO: print each sample next to getOrderAction(sample)ရလဒ်: Output က ဒီလို ဖြစ်ရပါမယ်: "paid" => Pack the items " SHIPPED " => Share tracking link "out-for-delivery" => Share tracking link "cancelled" => Refund the customer "lost" => Unknown status: lost
အရိပ်အမြွက် ကြည့်ရန်
case တိုင်းမှာ return ကို သုံးရင် break ရေးစရာ မလိုပါဘူး။ Grouping အတွက် case "shipped": အောက်မှာ ဘာမှ မရေးဘဲ case "out-for-delivery": ကို ချက်ချင်း ဆက်ရေးပါ။
အဖြေ ကြည့်ရန်
function getOrderAction(status) {
const normalized = status.trim().toLowerCase();
switch (normalized) {
case "pending":
return "Send payment reminder";
case "paid":
return "Pack the items";
case "shipped":
case "out-for-delivery":
return "Share tracking link";
case "cancelled":
return "Refund the customer";
default:
return "Unknown status: " + status;
}
}
const samples = ["paid", " SHIPPED ", "out-for-delivery", "cancelled", "lost"];
for (const sample of samples) {
console.log(JSON.stringify(sample) + " => " + getOrderAction(sample));
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- switch က expression ကို တစ်ကြိမ် တွက်ပြီး case တွေကို === နဲ့ အပေါ်ကနေ အောက် စစ်ပါတယ်၊ type မပြောင်းပေးပါ။
- ကိုက်တဲ့ case ကနေ break ဒါမှမဟုတ် return မရောက်မချင်း အောက်ကို ဆက် run ပါတယ် (fallthrough)။
- Label တွေကို ကပ်ရေးရင် code တစ်ခုကို မျှသုံးနိုင်ပြီး default က မသိတဲ့ value အတွက် policy ပါ။
- Range တွေအတွက် if...else ကို၊ ရိုးရှင်းတဲ့ mapping အတွက် lookup object ကို ရွေးပါ။
MDN — JavaScript Guide — MDN Web Docs