ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
Operator ဆိုတာ value တွေကို ယူပြီး value အသစ်တစ်ခု ထုတ်ပေးတဲ့ သင်္ကေတလေးတွေပါ။ ဂဏန်းပေါင်းစက်မှာ +၊ -၊ ×၊ ÷ ခလုတ်တွေ ရှိသလိုပဲ JavaScript မှာလည်း တွက်ဖို့ (+ - * /)၊ နှိုင်းယှဉ်ဖို့ (> < ===) နဲ့ "နှင့်"၊ "သို့မဟုတ်" ဆိုတဲ့ ဆုံးဖြတ်ချက် ပေါင်းဖို့ (&& ||) operator တွေ ရှိပါတယ်။
ဂဏန်းပေါင်းစက်နဲ့ ကွာတာက JavaScript operator တွေက type မတူတဲ့ value တွေကို ရောပေးရင်လည်း error မပြဘဲ တစ်ခုခု လုပ်ပေးတာပါ။ "5" + 5 က 10 မဟုတ်ဘဲ "55" ဖြစ်ပါတယ်။ ဒီ lesson ရဲ့ အဓိက အလုပ်က ဒီလို အံ့ဩစရာတွေကို ကြိုသိထားဖို့ပါ။
Problem → Why → Solution
ပြဿနာ: == က bug ကို ဖုံးထားတယ်
Checkout page တစ်ခုမှာ ပစ္စည်းဈေး 10 နဲ့ form ကနေ ရတဲ့ shipping ခ "5" ကို ပေါင်းပါတယ်။ ရလဒ်က 15 မဟုတ်ဘဲ "105" ပါ။ ဒါပေမယ့် test တစ်ခုက total == 105 လို့ စစ်ထားလို့ pass ဖြစ်နေပြီး developer က ဒါကို မှန်တယ်လို့ ထင်နေပါတယ်။ Total ကို နောက်ထပ် တွက်ချက်မှုတစ်ခုမှာ သုံးမှ ဈေးနှုန်း အကုန် ရူးသွပ်သွားပါတယ်။
const price = 10;
const shipping = "5"; // came from a form field
const total = price + shipping; // "105": + joined the text
console.log(total == 105); // true - hides the bug
console.log(total === 105); // false - reveals itဘာကြောင့် == က true ပြန်တာလဲ
== (loose equality) က နှိုင်းယှဉ်ခင် ဘက်နှစ်ဖက်ကို type တူအောင် ပြောင်းပါတယ်၊ ဒါကြောင့် string "105" နဲ့ number 105 က တူတယ်လို့ ထင်ရပါတယ်။ === (strict equality) ကတော့ ဘာမှ မပြောင်းပါဘူး — type မတူရင် ဘယ်တော့မှ မတူပါဘူး။ ဒါကြောင့် === က bug ကို ချက်ချင်း ဖော်ပြပေးပြီး == ကတော့ အဲဒါကို နောက်ပိုင်း ပိုဆိုးတဲ့ နေရာအထိ သယ်သွားပါတယ်။
Core concept: operand တွေ ဝင်၊ value တစ်ခု ထွက်
Operator တစ်ခုက operand လို့ ခေါ်တဲ့ value တစ်ခု သို့မဟုတ် နှစ်ခုကို ယူပြီး value အသစ်တစ်ခု ထုတ်ပေးပါတယ်၊ ဒါကြောင့် operator ပါတဲ့ code တိုင်းက expression တစ်ခုပါ။ ! နဲ့ -x ထဲက minus လို unary operator တွေက operand တစ်ခုပဲ ယူပြီး + နဲ့ === လို binary operator တွေက နှစ်ခု ယူပါတယ်။ အသုံးများတဲ့ အုပ်စု လေးစုကို အောက်က table မှာ ကြည့်ပါ။
| အုပ်စု | Operator | ဥပမာ → ရလဒ် |
|---|---|---|
| Arithmetic | + - * / % ** | 17 % 5 → 2, 2 ** 3 → 8 |
| Assignment | = += -= *= ++ -- | total += 20 (100 → 120) |
| Comparison | === !== > < >= <= | 5 === "5" → false |
| Logical | && || ! | true && false → false |
+ က အလုပ် နှစ်မျိုး လုပ်ပါတယ်။ ဘက်နှစ်ဖက်လုံး number ဖြစ်ရင် ပေါင်းပြီး တစ်ဖက်ဖက်က string ဖြစ်ရင် စာသား ဆက်ပါတယ်။ ဘယ်ကနေ ညာကို တစ်ဆင့်ချင်း တွက်လို့ 1 + 2 + "3" က "33" (အရင် 3၊ ပြီးမှ "3" နဲ့ ဆက်) ဖြစ်ပြီး "1" + 2 + 3 ကတော့ "123" ဖြစ်ပါတယ်။ % က စားပြီး ကျန်တဲ့ အကြွင်းကို ပေးလို့ စုံ/မ စစ်ဖို့ (n % 2 === 0) အသုံးဝင်ပြီး ** က ထပ်ကိန်းပါ။
== နဲ့ != က type ကို ပြောင်းပြီးမှ နှိုင်းယှဉ်လို့ 5 == "5"၊ 0 == "" နဲ့ null == undefined အားလုံး true ဖြစ်ပါတယ်။ === နဲ့ !== ကတော့ type နဲ့ value နှစ်ခုလုံး တူမှ true ပါ၊ ဒါကြောင့် အမြဲ === နဲ့ !== ကို သုံးပါ။ && နဲ့ || က short-circuit လုပ်ပါတယ် — && က falsy value တစ်ခု တွေ့တာနဲ့ ရပ်ပြီး || က truthy value တစ်ခု တွေ့တာနဲ့ ရပ်ကာ boolean မဟုတ်ဘဲ ရပ်သွားတဲ့ operand ကိုယ်တိုင်ကို ပြန်ပေးပါတယ်၊ ဒါကြောင့် "" || "Guest" က "Guest" ဖြစ်ပါတယ်။ Conditional operator condition ? a : b က condition မှန်ရင် a ကို၊ မှားရင် b ကို ပေးပါတယ်။ Precedence အရ * နဲ့ / က + နဲ့ - ထက် အရင်၊ && က || ထက် အရင် တွက်ပါတယ် — သံသယ ဖြစ်ရင် ကွင်း ထည့်ပါ။
- Operand တွေ ဝင်ပြီး value တစ်ခု ထွက်
- တစ်ဖက်က string ဆိုရင် + က ဆက်တယ်
- === က type ကော value ကော စစ်တယ်
- && / || က စောစော ရပ်ပြီး operand ပြန်ပေး
သံသယရှိရင် ကွင်း ထည့်ပါ၊ နှိုင်းယှဉ်ရင် === ကို သုံးပါ။
အထွေထွေ ပုံစံ
result = a + b; // arithmetic: + - * / % **
total += amount; // assignment shortcut: += -= *= /=
a === b; a !== b; // strict comparison, no type conversion
a > b; a <= b; // relational comparison
x && y; x || y; !x; // logical and, or, not
value ?? fallback; // fallback only for null or undefined
condition ? ifTrue : ifFalse; // conditional (ternary) operatorOperator ရဲ့ ဘယ်ညာက value တွေကို operand လို့ ခေါ်ပါတယ်။ += လို shortcut တွေက total = total + amount ကို တိုတိုရေးတာပါ။ ?? ကို အောက်က deeper section မှာ || နဲ့ ယှဉ်ကြည့်ပါမယ်။
Example: operator အုပ်စု တစ်ခုစီ
ဒီ example က ရလဒ်တွေအပြင် x နဲ့ y ကိုလည်း print ထုတ်ပြီး operator အုပ်စု လေးစုလုံးကို line တစ်ကြောင်းစီ ပြပါတယ်။ Line တစ်ကြောင်းချင်းကို run မလုပ်ခင် ခန့်မှန်းကြည့်ပါ။
let x = 5 + 5; // 10
let y = "5" + 5; // "55" (string joining)
let z = 5 == "5"; // true (loose equality converts types)
let a = 5 === "5"; // false (strict equality)
console.log(x, y);
console.log(z, a);
console.log(17 % 5, 2 ** 3);
let total = 100;
total += 20;
console.log(total);
const age = 20;
const hasTicket = false;
console.log(age >= 18 && hasTicket, age >= 18 || hasTicket, !hasTicket);Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Line 2
let y = "5" + 5; // "55" (string joining)တစ်ဖက်က string ဖြစ်လို့ + က number 5 ကို စာသား "5" အဖြစ် ပြောင်းပြီး ဆက်ပါတယ်။ console.log က string ကို quote မပါဘဲ ပြလို့ output မှာ 55 လို့ပဲ မြင်ရပါတယ်။
- Lines 3-4
let a = 5 === "5"; // false (strict equality)== က "5" ကို 5 အဖြစ် ပြောင်းပြီး true ပေးပေမယ့် === က number နဲ့ string type မတူတာကို တွေ့တာနဲ့ false ပေးပါတယ်။
- Line 8
console.log(17 % 5, 2 ** 3);17 ကို 5 နဲ့ စားရင် 3 ကြိမ် ဝင်ပြီး 2 ကြွင်းလို့ 17 % 5 က 2 ဖြစ်ပါတယ်။ 2 ** 3 က 2 × 2 × 2 = 8 ပါ။
- Line 11
total += 20;total = total + 20; ရဲ့ အတိုကောက်ပါ။ total ကို reassign လုပ်လို့ const မဟုတ်ဘဲ let နဲ့ ကြေညာထားရပါတယ်။
- Line 16
console.log(age >= 18 && hasTicket, age >= 18 || hasTicket, !hasTicket);age >= 18 က true ပါ။ && က နှစ်ဖက်လုံး truthy ဖြစ်မှ true ဖြစ်လို့ false ရပြီး || က တစ်ဖက်ဖက် truthy ဖြစ်ရင် ရလို့ true ရပါတယ်။ ! က false ကို true အဖြစ် ပြောင်းပါတယ်။
10 55
true false
2 8
120
false true trueShort-circuit default တွေနဲ့ ??
|| ကို default value ပေးဖို့ သုံးလေ့ရှိပေမယ့် 0၊ "" နဲ့ false လို falsy value တိုင်းကို အစားထိုးလိုက်ပါတယ်။ Volume 0 ဒါမှမဟုတ် နာမည်အလွတ်လို တကယ် အဓိပ္ပာယ်ရှိတဲ့ value တွေကိုပါ ဖျက်ပစ်တာပါ။ ?? (nullish coalescing) ကတော့ null နဲ့ undefined ကိုပဲ အစားထိုးလို့ အဲဒီ value တွေကို ထိန်းထားပေးပါတယ်။
const savedName = "";
console.log(savedName || "Guest"); // Guest: "" is falsy
console.log("[" + (savedName ?? "Guest") + "]"); // []: "" is kept
const volume = 0;
console.log(volume || 50, volume ?? 50); // 50 0
let count = 5;
count++;
console.log(count, 10 % 3, 2 + 3 * 4, (2 + 3) * 4); // 6 1 14 20နောက်ဆုံး line က precedence ကို ပြပါတယ် — 2 + 3 * 4 မှာ မြှောက်တာ အရင်ဖြစ်လို့ 14 ရပြီး ကွင်းထည့်လိုက်မှ 20 ရပါတယ်။
လက်တွေ့ project တွေမှာ
Real project တွေမှာ ဘယ်လို သုံးလဲ
Shopping cart တိုင်းက * နဲ့ += ကို သုံးပြီး subtotal တွက်ပါတယ်။ Pagination မှာ item 47 ခုကို page တစ်ခု 10 ခုနဲ့ ခွဲရင် page 5 ခု လိုတယ်ဆိုတာ Math.ceil(47 / 10) နဲ့ တွက်ပြီး table တွေမှာ row တစ်ကြောင်းကျော် အရောင်ပြောင်းဖို့ index % 2 ကို သုံးပါတယ်။ user && user.isAdmin လို && စစ်ချက်တွေက feature ကို admin တွေကိုပဲ ပြပေးပါတယ်။ Game code တွေကလည်း ဒီ operator တွေကို အားကိုးပါတယ် — ထိတိုင်း score += points၊ ဖမ်းမိတိုင်း lives--၊ level ငါးခုတိုင်း boss round စဖို့ level % 5 === 0 စတာတွေပါ။
Config value တွေမှာ ?? က အရေးကြီးပါတယ် — const timeout = options.timeout ?? 5000; က user က timeout: 0 လို့ တမင် ပေးထားရင် 0 ကို ထိန်းထားပေးပြီး || ကတော့ 5000 နဲ့ အစားထိုးလိုက်ပါမယ်။ Team အများစုက ESLint ရဲ့ eqeqeq rule ကို ဖွင့်ထားလို့ == ကို ရေးမိရင် === ပြောင်းဖို့ editor က ချက်ချင်း သတိပေးပါတယ်။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. == ကို သုံးပြီး အံ့ဩစရာ true တွေ ရတာ
0 == ""၊ "1" == true နဲ့ null == undefined အားလုံး true ပါ။ ဒီလို conversion စည်းမျဉ်းတွေကို မှတ်နေမယ့်အစား === နဲ့ !== ကိုပဲ သုံးပြီး type ပြောင်းဖို့ လိုရင် Number() နဲ့ ကိုယ်တိုင် ပြောင်းပါ။
2. + ရဲ့ ဘယ်ကနေ ညာ တွက်ပုံကို မေ့တာ
"Total: " + 10 + 5 က "Total: 15" မဟုတ်ဘဲ "Total: 105" ပါ၊ string က ရှေ့ဆုံးမှာ ရှိလို့ နောက်က + တွေ အကုန် စာသား ဆက်ပါတယ်။ "Total: " + (10 + 5) လို ကွင်းခတ်ပါ၊ ဒါမှမဟုတ် template literal ထဲမှာ ${10 + 5} လို့ ရေးပါ။
3. 0 ဒါမှမဟုတ် "" က မှန်ကန်တဲ့ value ဖြစ်နေတုန်း || နဲ့ default ပေးတာ
const quantity = input || 1; က user က 0 ရိုက်ထည့်ရင်လည်း 1 ဖြစ်သွားပါတယ်။ null နဲ့ undefined ကိုပဲ အစားထိုးချင်ရင် ?? ကို သုံးပါ။
4. Precedence ကို ခန့်မှန်းမှားတာ
Average တွက်ဖို့ a + b / 2 လို့ ရေးရင် b ကိုပဲ 2 နဲ့ စားပြီးမှ a နဲ့ ပေါင်းပါတယ်။ (a + b) / 2 လို့ ကွင်းခတ်ပါ။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- နှိုင်းယှဉ်တိုင်း === နဲ့ !== ကို သုံးပါ၊ == ကို ရည်ရွယ်ချက်ရှိရှိ သုံးတဲ့ နေရာ ရှားပါတယ်။
- String နဲ့ ဂဏန်း ရောတဲ့ စာသားအတွက် + အစား template literal ကို သုံးပါ။
- Operator သုံးလေးခု ရောနေတဲ့ expression မှာ ကွင်းထည့်ပြီး ရည်ရွယ်ချက်ကို ရှင်းရှင်းလင်းလင်း ပြပါ။
Mini challenge
Mini challenge: checkout calculator
if statement မသုံးဘဲ operator တွေနဲ့ပဲ shop တစ်ခုရဲ့ checkout ရလဒ်တွေကို တွက်ပါ။
- subtotal = price * quantity (12.5 × 4)
- Member ဖြစ်ပြီး subtotal >= 40 ဆိုရင် 10% discount (&& နဲ့ ? : သုံး)
- subtotal >= 60 ဒါမှမဟုတ် member ဖြစ်ရင် free shipping (|| သုံး)
- couponCode အလွတ်ဆိုရင် "NONE" ပြပြီး quantity စုံကိန်းလား % နဲ့ စစ်ရမယ်
const price = 12.5;
const quantity = 4;
const isMember = true;
const couponCode = "";
// TODO: subtotal
// TODO: getsDiscount - a member AND subtotal of at least 40
// TODO: total - 90% of subtotal when getsDiscount, otherwise subtotal
// TODO: freeShipping - subtotal of at least 60 OR a member
// TODO: code - couponCode, or "NONE" when it is empty
// TODO: print four lines: subtotal, discount + total,
// free shipping + coupon, and whether quantity is evenရလဒ်: Line လေးကြောင်း ထွက်ပါမယ်: Subtotal: 50 Discount: true Total: 45 Free shipping: true Coupon: NONE Even quantity: true
အရိပ်အမြွက် ကြည့်ရန်
getsDiscount ? subtotal * 0.9 : subtotal လို conditional operator ကို သုံးပါ။ "" က falsy ဖြစ်လို့ couponCode || "NONE" က "NONE" ပေးပါတယ်။
အဖြေ ကြည့်ရန်
const price = 12.5;
const quantity = 4;
const isMember = true;
const couponCode = "";
const subtotal = price * quantity;
const getsDiscount = isMember && subtotal >= 40;
const total = getsDiscount ? subtotal * 0.9 : subtotal;
const freeShipping = subtotal >= 60 || isMember;
const code = couponCode || "NONE";
console.log("Subtotal:", subtotal);
console.log("Discount:", getsDiscount, "Total:", total);
console.log("Free shipping:", freeShipping, "Coupon:", code);
console.log("Even quantity:", quantity % 2 === 0);Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Operator တွေက operand တွေကို ယူပြီး value အသစ် ထုတ်ပေးပါတယ်: arithmetic၊ assignment၊ comparison၊ logical။
- + က တစ်ဖက်ဖက်မှာ string ပါရင် စာသား ဆက်ပြီး ဘယ်ကနေ ညာ တွက်ပါတယ်။
- == က type ပြောင်းပြီးမှ နှိုင်းယှဉ်လို့ === နဲ့ !== ကို အမြဲ သုံးပါ။
- && နဲ့ || က short-circuit လုပ်ပြီး operand ကို ပြန်ပေးပါတယ်၊ 0 နဲ့ "" ကို ထိန်းထားချင်ရင် ?? သုံးပါ။
MDN — JavaScript Guide — MDN Web Docs