Thuta Learning
JavaScript
BasicProgrammingအခြေခံ

JS Variables (var, let, const)

JavaScriptသင်ခန်းစာ 425 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • const ကို default အဖြစ်သုံးပြီး reassign လုပ်ရမှသာ let ကို ရွေးနိုင်ဖို့
  • const က reassign ကို တားပေမယ့် object/array ထဲက value ပြောင်းတာကို မတားကြောင်း ရှင်းပြနိုင်ဖို့
  • let/const ရဲ့ block scope နဲ့ var ရဲ့ function scope ကွာခြားချက်ကို ပြနိုင်ဖို့
  • "Cannot access ... before initialization" နဲ့ "Assignment to constant variable" error တွေကို ဖတ်ပြီး ပြင်နိုင်ဖို့

ရိုးရိုးလေး ရှင်းပြမယ်

ရိုးရိုးလေး ရှင်းရရင်

Variable ဆိုတာ value တစ်ခုပေါ်မှာ ကပ်ထားတဲ့ နာမည် တံဆိပ်လေးပါ။ "Alice" လို value တစ်ခုကို name ဆိုတဲ့ တံဆိပ်နဲ့ ချိတ်ထားရင် နောက်ပိုင်း name လို့ ခေါ်တာနဲ့ အဲဒီ value ကို ပြန်ရပါတယ်။

JavaScript မှာ တံဆိပ် အမျိုးအစား သုံးမျိုး ရှိပါတယ်။ const က ကော်နဲ့ အသေကပ်ထားတဲ့ တံဆိပ်ပါ — တခြား value ဆီ ရွှေ့လို့ မရပါဘူး။ let က ခွာပြီး တခြား value ပေါ် ပြန်ကပ်လို့ရတဲ့ တံဆိပ်ပါ။ var ကတော့ ခေတ်ဟောင်း တံဆိပ်ဖြစ်ပြီး ထင်မထားတဲ့ နေရာတွေအထိ ပေါက်ကြားတတ်လို့ code အသစ်မှာ မသုံးတော့ပါဘူး။

Problem → Why → Solution

ပြဿနာ: var က value ဟောင်းကို တိတ်တိတ်လေး ဖျက်ပစ်တယ်

File ရှည်ရှည် တစ်ခုမှာ အပေါ်ပိုင်းက total ကို 100 လို့ သတ်မှတ်ထားပါတယ်။ Line နှစ်ရာလောက် အောက်မှာ တခြားတစ်ယောက်က တခြားအလုပ်အတွက် var total ကို ထပ်ကြေညာလိုက်ပါတယ်။ JavaScript က error မပြဘဲ လက်ခံလိုက်လို့ မူလ total ပျောက်သွားပြီး order စာရင်း တွက်ချက်မှုတွေ မှားသွားပါတယ် — ဘယ်သူမှလည်း သတိမထားမိပါဘူး။

orders.js
var total = 100;
// ... 200 lines later, the same name is reused
var total = 0;        // no error: var allows declaring it again
console.log(total);   // 0 - the original 100 is silently gone

let price = 100;
let price = 0;        // SyntaxError: Identifier 'price' has already been declared

ဘာကြောင့် let နဲ့ const ကို သုံးသင့်တာလဲ

var က scope တစ်ခုတည်းမှာ နာမည်တစ်ခုကို အကြိမ်ကြိမ် ကြေညာခွင့်ပြုပြီး if နဲ့ for block တွေကိုလည်း ဖောက်ထွက်ပါတယ်။ ES2015 က ထည့်ပေးတဲ့ let နဲ့ const ကတော့ ထပ်ကြေညာရင် code မ run ခင်ကတည်းက SyntaxError ပြပြီး block ထဲမှာပဲ နေပါတယ်။ const က နောက်ထပ် အဆင့်တစ်ဆင့် ထပ်ကာကွယ်ပါတယ် — ပြောင်းဖို့ မရည်ရွယ်တဲ့ value ကို မတော်တဆ reassign လုပ်မိရင် TypeError ပေးပါတယ်။

Core concept: binding၊ scope နဲ့ reassignment

Variable ကြေညာတယ်ဆိုတာ binding တစ်ခု ဖန်တီးတာပါ — နာမည်တစ်ခုနဲ့ value တစ်ခုကြား ချိတ်ဆက်မှုပါ။ let name = "Alice"; က binding ကို ဖန်တီးပြီး name = "Bob"; က တံဆိပ်ကို value အသစ်ဆီ ရွှေ့ပါတယ်၊ ဒါကို reassignment လို့ ခေါ်ပါတယ်။ const binding ကိုတော့ ရွှေ့လို့ မရလို့ ကြေညာကတည်းက value ပေးရပါတယ်၊ const x; လို့ ရေးရင် SyntaxError ဖြစ်ပါတယ်။

let မှာတော့ ကြေညာတာနဲ့ value ထည့်တာကို ခွဲလုပ်လို့ ရပါတယ်။ let message; က binding ကို undefined value နဲ့ ဖန်တီးပြီး နောက်မှ assign လုပ်လို့ရပါတယ် — value က if/else လို နောက်မှ ဆုံးဖြတ်ချက်ပေါ် မူတည်တဲ့အခါ အသုံးဝင်ပါတယ်။ Statement တစ်ခုတည်းမှာ let a = 1, b = 2; လို variable အများကြီး ကြေညာလို့ရပေမယ့် line တစ်ကြောင်းမှာ တစ်ခုစီ ရေးတာက ဖတ်ရတာရော code review မှာ ပြင်ရတာပါ ပိုလွယ်ပါတယ်။

KeywordReassign လုပ်လို့ ရလားScope
constမရ (TypeError)Block { }
letရBlock { }
varရ၊ ထပ်ကြေညာတောင် ရFunction တစ်ခုလုံး

const က binding ကို ကာကွယ်တာဖြစ်ပြီး value ကို ကာကွယ်တာ မဟုတ်ပါဘူး။ const cart = ["apple"]; ဆိုရင် cart.push("banana") က array တစ်ခုတည်းထဲကို item ထည့်တာမို့ ရပါတယ် (mutation)။ cart = [] ကတော့ တံဆိပ်ကို array အသစ်ဆီ ရွှေ့ဖို့ ကြိုးစားတာမို့ TypeError ဖြစ်ပါတယ်။ Number နဲ့ string လို primitive value တွေက ပြောင်းလို့ မရတာမို့ const number တစ်ခုကတော့ တကယ်ပဲ ပုံသေ ဖြစ်ပါတယ်။

let နဲ့ const က သူတို့ကို ကြေညာထားတဲ့ { } block ထဲမှာပဲ ရှိပါတယ်။ Block စတာနဲ့ နာမည်က ရှိနေပေမယ့် ကြေညာတဲ့ line ကို မရောက်ခင် ဖတ်ရင် ReferenceError: Cannot access 'x' before initialization ဖြစ်ပါတယ် — ဒီကြားကာလကို temporal dead zone (TDZ) လို့ ခေါ်ပါတယ်။ var ကတော့ function ရဲ့ အစကို hoist ဖြစ်ပြီး undefined အဖြစ် ကြိုရှိနေလို့ မကြေညာခင် ဖတ်ရင် error မပြဘဲ undefined ကို ပေးပါတယ် — bug ကို ဖုံးကွယ်ထားတာပါ။ စည်းမျဉ်းကို ဒီလို မှတ်ပါ — const ကို default သုံး၊ reassign လုပ်ရမှ let၊ code အသစ်မှာ var ကို ဘယ်တော့မှ မသုံးပါနဲ့။

text
Reassignment moves the tag; mutation changes the value
------------------------------------------------------
let name = "Alice"      name --> "Alice"
name = "Bob"            name --> "Bob"        (tag moved: allowed for let)

const cart = ["apple"]  cart --> [ "apple" ]
cart.push("banana")     cart --> [ "apple", "banana" ]  (same array: OK)
cart = []               TypeError: Assignment to constant variable.
မှတ်ထားရမယ့် ပုံစံ: keyword ရွေးနည်း
  1. const နဲ့ အမြဲ စတင်ပါ
  2. Reassign လုပ်ရမှသာ let ကို ပြောင်းပါ
  3. Block အပြင်မှာ နာမည် မရှိဘူးလို့ မှတ်ပါ
  4. Code အသစ်မှာ var ကို မသုံးပါနဲ့

const = တံဆိပ် မရွှေ့ရ၊ ဒါပေမယ့် object/array ထဲက value ပြောင်းလို့ ရပါတယ်။

အထွေထွေ ပုံစံ

javascript
const fixedName = initialValue;   // must be initialised, never reassigned
let changingName = initialValue;  // may be reassigned later
changingName = newValue;

{
  const insideOnly = value;       // exists only inside this block
}
var legacyName = value;           // old style: function-scoped

Keyword၊ နာမည်၊ = နဲ့ value ဆိုတဲ့ ပုံစံ တူတူပါပဲ။ ကွာတာက binding ကို ရွှေ့လို့ရလား (let) မရဘူးလား (const) နဲ့ နာမည်က ဘယ်အထိ မြင်ရလဲ (block သို့ function) ဆိုတာပါ။

Example: keyword သုံးမျိုး လက်တွေ့

const ကို reassign လုပ်ရင် TypeError တက်ပြီး ပုံမှန်ဆိုရင် script ရပ်သွားပါတယ်။ ဒီမှာတော့ try/catch နဲ့ အဲဒီ error ကို ဖမ်းပြီး message ကို print ထုတ်ထားလို့ program ဆက် run ပါတယ် — try/catch ကို Intermediate chapter မှာ အသေးစိတ် သင်ပါမယ်။ const array ကို push လုပ်တာ အလုပ်လုပ်တာကိုလည်း သတိထားကြည့်ပါ။

variables.js
let name = "Alice";       // let: this value will change
name = "Bob";
console.log(name);

const birthYear = 2000;   // const: never reassigned
try {
  birthYear = 2001;
} catch (error) {
  console.log(error.name + ": " + error.message);
}

const cart = ["apple"];   // const blocks reassignment, not changes inside
cart.push("banana");
console.log(cart);

var city = "Yangon";      // var: the old, function-scoped way
console.log(city);

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. Lines 1-3name = "Bob";

    let နဲ့ ကြေညာထားလို့ name ကို value အသစ် "Bob" ဆီ ရွှေ့လို့ ရပါတယ်။ Reassign လုပ်တဲ့အခါ let ကို ထပ်မရေးရပါဘူး — ထပ်ရေးရင် SyntaxError ဖြစ်ပါတယ်။

  2. Line 7birthYear = 2001;

    const binding ကို ရွှေ့ဖို့ ကြိုးစားတာမို့ run နေတုန်း TypeError ဖြစ်ပါတယ်။ catch block က error.name (TypeError) နဲ့ error.message ကို print ထုတ်ပါတယ်။

  3. Line 13cart.push("banana");

    Array အသစ် မဖန်တီးဘဲ ရှိပြီးသား array ထဲကို item ထည့်တာ (mutation) ဖြစ်လို့ const နဲ့ ရပါတယ်။ Node.js က array ကို [ 'apple', 'banana' ] ပုံစံနဲ့ ပြပါတယ်။

  4. Line 16var city = "Yangon";

    var က ဒီနေရာမှာ let လိုပဲ အလုပ်လုပ်ပေမယ့် block ထဲမှာ ကြေညာရင် block အပြင်ကို ပေါက်ကြားပြီး ထပ်ကြေညာတာကိုလည်း ခွင့်ပြုပါတယ်။ Code ဟောင်းတွေမှာ တွေ့ရင် နားလည်ဖို့ပဲ သိထားပါ။

You should see
Bob
TypeError: Assignment to constant variable.
[ 'apple', 'banana' ]
Yangon

Block scope နဲ့ hoisting ကို မျက်မြင်

အောက်က code မှာ if block ထဲမှာ var နဲ့ let ကို တစ်ခုစီ ကြေညာထားပါတယ်။ var က block အပြင်ကနေ မြင်ရပေမယ့် let ကတော့ မမြင်ရပါဘူး။ ပြီးတော့ var ကို မကြေညာခင် ဖတ်တာက undefined ပေးပြီး let ကို မကြေညာခင် ဖတ်တာကတော့ TDZ error ပေးပါတယ်။

scope.js
if (true) {
  var oldStyle = "visible outside";
  let newStyle = "only inside";
}
console.log(oldStyle);          // visible outside
console.log(typeof newStyle);   // undefined: no such name out here

console.log(early);             // undefined: var is hoisted
var early = 1;

try {
  console.log(later);           // TDZ
  let later = 2;
} catch (error) {
  console.log(error.message);   // Cannot access 'later' before initialization
}

typeof newStyle က error မပြတာကို သတိထားပါ — လုံးဝ မရှိတဲ့ နာမည်ပေါ်မှာ typeof သုံးရင် "undefined" ကို ပြန်ပါတယ်။ TDZ ထဲမှာတော့ ကွာပါတယ်၊ နာမည်က ရှိနေပြီ ဖြစ်ပေမယ့် အသုံးပြုဖို့ အဆင်သင့် မဖြစ်သေးလို့ပါ။ Code ဟောင်းတွေက loop ပြီးသွားတဲ့နောက် loop counter ကို ဖတ်တာလို var ရဲ့ ပေါက်ကြားတဲ့ scope ကို အားကိုးခဲ့ကြပါတယ်၊ let နဲ့ဆိုရင်တော့ အဲဒီလို value ကို loop ရဲ့ အပေါ်မှာ ကြေညာပါ။

လက်တွေ့ project တွေမှာ

Real project တွေမှာ ဘယ်လို သုံးလဲ

ခေတ်မီ codebase တွေမှာ variable အများစုက const ပါ — function တွေ၊ import လုပ်ထားတဲ့ module တွေ၊ API ကနေ ရတဲ့ data တွေ၊ config value တွေ အကုန်လုံးပါ။ let ကို counter၊ running total နဲ့ loop တစ်ခုမှာ ပြောင်းနေတဲ့ value လို တကယ် ပြောင်းရတဲ့ နေရာအနည်းငယ်မှာပဲ တွေ့ရပါတယ်။ ဒါကြောင့် code ဖတ်တဲ့သူက let ကို မြင်တာနဲ့ "ဒီ value နောက်ပိုင်း ပြောင်းမယ်" ဆိုတဲ့ signal ရပါတယ်။ Code review လည်း ပိုမြန်ပါတယ် — value တစ်ခုက const ဆိုရင် ကြေညာတဲ့နေရာကနေ block အဆုံးအထိ အရာတစ်ခုတည်းကို ကိုင်ထားတယ်ဆိုတာ reviewer သိလို့ နောက်ပိုင်း assign လုပ်တာတွေ လိုက်ရှာစရာ မလိုပါဘူး။

ESLint ရဲ့ prefer-const rule က reassign မလုပ်တဲ့ let တွေကို const ပြောင်းခိုင်းပြီး no-var rule က var ကို လုံးဝ တားပါတယ်။ API_URL၊ MAX_UPLOAD_MB လို program တစ်ခုလုံးမှာ ပုံသေ ဖြစ်တဲ့ value တွေကို UPPER_SNAKE_CASE နာမည်နဲ့ const အဖြစ် file ရဲ့ ထိပ်မှာ စုထားလေ့ ရှိပါတယ်။

အမှားများ

အဖြစ်များတဲ့ အမှားတွေ

1. const က object ကို ပုံသေ လုပ်ပေးတယ်လို့ ထင်တာ

const user = { name: "Mya" }; ပြီးနောက် user.name = "Hla"; က error မပြဘဲ အလုပ်လုပ်ပါတယ်။ Object ကို တကယ် ပြောင်းလို့မရအောင် လုပ်ချင်ရင် Object.freeze(user) ကို သုံးပါ၊ ဒါပေမယ့် အဲဒါက အပေါ်ဆုံး level ကိုပဲ freeze လုပ်ပါတယ်။

2. let variable ကို မကြေညာခင် သုံးတာ

console.log(total); let total = 5; က ReferenceError: Cannot access 'total' before initialization ပေးပါတယ်။ var နဲ့ဆိုရင် undefined ကို တိတ်တိတ်လေး ရမှာမို့ ဒီ error က တကယ်တော့ အကူအညီပါ — ကြေညာချက်ကို အသုံးပြုတဲ့ line ရဲ့ အပေါ်ကို ရွှေ့ပါ။

3. Keyword မပါဘဲ assign လုပ်တာ

score = 10; လို့ ကြေညာချက်မပါဘဲ ရေးရင် strict mode မဟုတ်တဲ့ script တွေမှာ global variable တစ်ခု မသိမသာ ဖြစ်သွားပြီး module နဲ့ strict mode မှာတော့ ReferenceError ဖြစ်ပါတယ်။ ပထမဆုံး သုံးတဲ့ နေရာမှာ const ဒါမှမဟုတ် let ကို အမြဲ ရေးပါ။

4. Loop counter အတွက် var သုံးတာ

for (var i = 0; i < 3; i++) ပြီးသွားရင် i က loop အပြင်မှာ 3 အဖြစ် ကျန်နေပါတယ်။ Loop ထဲမှာ setTimeout callback တွေ ထားရင် callback အားလုံးက i တစ်ခုတည်းကို မျှသုံးလို့ 3 3 3 လို့ print ထုတ်ပါတယ်။ let i နဲ့ဆိုရင် iteration တစ်ခုစီက ကိုယ်ပိုင် i ရလို့ 0 1 2 ထွက်ပါတယ်။

Best Practices

အကောင်းဆုံး အလေ့အကျင့်များ

  • Variable ကို သုံးမယ့်နေရာနဲ့ အနီးဆုံး၊ အကျဉ်းဆုံး block ထဲမှာ ကြေညာပါ။
  • isLoggedIn၊ cartTotal လို value ရဲ့ အဓိပ္ပာယ်ကို ပြောပြတဲ့ နာမည်ပေးပါ၊ data၊ temp၊ x လို နာမည်တွေကို ရှောင်ပါ။
  • Editor ထဲမှာ ESLint ရဲ့ prefer-const နဲ့ no-var ကို ဖွင့်ထားပါ၊ ဒါမှ keyword ရွေးတာကို tool က စစ်ပေးပါမယ်။

Mini challenge

Mini challenge: game player ရဲ့ state

Game တစ်ခုအတွက် player state ကို variable တွေနဲ့ ကိုင်တွယ်ပါ၊ variable တစ်ခုချင်းအတွက် မှန်ကန်တဲ့ keyword ကို ရွေးပါ။

  • playerName ("Mya") နဲ့ maxLives (3) က ဘယ်တော့မှ မပြောင်းလို့ const သုံးရမယ်
  • score (0) နဲ့ lives (maxLives ကနေ စ) က ပြောင်းမှာမို့ let သုံးရမယ်
  • inventory ကို const array အလွတ်အဖြစ် ကြေညာပြီး "key" ကို ထည့်ရမယ်
  • Score 10 မှတ် နှစ်ကြိမ် ထည့်၊ life တစ်ခု လျှော့ပြီး status line နှစ်ကြောင်း print ထုတ်ရမယ်
player.js
// TODO: playerName and maxLives - values that never change

// TODO: score and lives - values that will be reassigned

// TODO: inventory - one array whose contents will grow

// TODO: score 10 points twice, lose one life, pick up a "key"

// TODO: print "Mya: 20 points, 2/3 lives" and "Inventory: key"

ရလဒ်: Line နှစ်ကြောင်း ထွက်ပါမယ်: Mya: 20 points, 2/3 lives Inventory: key

အရိပ်အမြွက် ကြည့်ရန်

score = score + 10; လို reassign လုပ်တာက let လိုပေမယ့် inventory.push("key") က mutation ဖြစ်လို့ const နဲ့ ရပါတယ်။ Array ကို စာသားအဖြစ် ပြဖို့ inventory.join(", ") ကို သုံးပါ။

အဖြေ ကြည့်ရန်
player.js
const playerName = "Mya";
const maxLives = 3;
let score = 0;
let lives = maxLives;
const inventory = [];

score = score + 10;
score = score + 10;
lives = lives - 1;
inventory.push("key");

console.log(`${playerName}: ${score} points, ${lives}/${maxLives} lives`);
console.log("Inventory: " + inventory.join(", "));

Quick Quiz 1 / 3

const colors = ["red"]; colors.push("blue"); console.log(colors.length); က ဘာ print ထုတ်မလဲ။

Quick Quiz 2 / 3

console.log(count); let count = 1; ကို run ရင် ဘာဖြစ်မလဲ။

Quick Quiz 3 / 3

ဒီ snippet တွေထဲက ဘယ်ဟာက error မရှိဘဲ run မလဲ။

မှတ်ထားရမယ့် အချက်များ

  • Variable က value တစ်ခုနဲ့ ချိတ်ထားတဲ့ နာမည် (binding) ပါ။
  • const ကို default သုံးပါ၊ reassign လုပ်ရမှ let၊ code အသစ်မှာ var မသုံးပါနဲ့။
  • const က reassign ကို တားပေမယ့် object နဲ့ array ထဲက value ပြောင်းတာကို မတားပါဘူး။
  • let/const က block scope ဖြစ်ပြီး ကြေညာချက် မရောက်ခင် ဖတ်ရင် TDZ ReferenceError ဖြစ်ပါတယ်။

MDN — JavaScript Guide — MDN Web Docs

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

JS Variables (var, let, const) | Thuta Learning