Thuta Learning
JavaScript
BasicProgrammingအခြေခံ

JS Syntax

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

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

  • Statement (လုပ်ဆောင်ချက်) နဲ့ expression (value ထုတ်ပေးတဲ့ code) ကို ခွဲခြားနိုင်ဖို့
  • Block၊ comment နဲ့ identifier (နာမည်) စည်းမျဉ်းတွေကို မှန်မှန်ကန်ကန် သုံးနိုင်ဖို့
  • JavaScript က case-sensitive ဖြစ်တာကြောင့် ဖြစ်တဲ့ bug တွေကို ရှာနိုင်ဖို့
  • Automatic semicolon insertion ရဲ့ return trap ကို ရှောင်နိုင်ဖို့

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

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

Syntax ဆိုတာ programming language တစ်ခုရဲ့ သဒ္ဒါပါ — engine နားလည်အောင် စကားလုံးနဲ့ သင်္ကေတတွေကို ဘယ်လို စီရမလဲဆိုတဲ့ စည်းမျဉ်းတွေပါ။ အင်္ဂလိပ်စာမှာ စာလုံးပေါင်း နည်းနည်းမှားရင်တောင် လူတွေ နားလည်ပါသေးတယ်။

JavaScript ကတော့ ပိုတင်းကျပ်ပါတယ်။ Quote တစ်ခု ဒါမှမဟုတ် } တစ်ခု ကျန်ခဲ့ရင် file တစ်ခုလုံး မ run တော့ပါဘူး၊ စာလုံးအကြီးအသေး မတူတဲ့ နာမည်ဆိုရင်လည်း လုံးဝ တခြားအရာ ဖြစ်သွားပါတယ်။ ကောင်းတာက စည်းမျဉ်း နည်းနည်းလေး — statement၊ expression၊ block၊ comment၊ case — ကို သိရုံနဲ့ ကိုယ်ရေးမယ့် code အားလုံးနီးပါးကို ဖုံးလွှမ်းပါတယ်။

Problem → Why → Solution

ပြဿနာ: စာလုံးတစ်လုံး မှားရုံနဲ့ program ရပ်သွားတယ်

အောက်က code မှာ အမှားနှစ်ခု ရှိပါတယ်။ username ကို u အသေးနဲ့ ရေးထားလို့ userName နဲ့ မတူပါဘူး။ if block ရဲ့ } လည်း ကျန်ခဲ့ပါတယ်။ အံ့ဩစရာက ဒီ file ကို run ရင် ReferenceError မမြင်ရဘဲ SyntaxError ကိုပဲ မြင်ရတာပါ — ပထမ line နှစ်ကြောင်းတောင် မ run ပါဘူး။

welcome.js
let userName = "Mya";
console.log(username);   // wrong case: ReferenceError... later

if (userName) {
  console.log("Welcome back");
// the closing } is missing

// $ node welcome.js
// SyntaxError: Unexpected end of input

ဘာကြောင့် SyntaxError ကိုပဲ မြင်ရတာလဲ

Engine က file ကို အဆင့်နှစ်ဆင့်နဲ့ ဖတ်ပါတယ်။ ပထမ file တစ်ခုလုံးကို parse လုပ်ပြီး structure တစ်ခုအဖြစ် ပြောင်းပါတယ် — ဒီအဆင့်မှာ သဒ္ဒါအမှား (SyntaxError) တွေ့ရင် line တစ်ကြောင်းမှ မ run ခင် အားလုံး ရပ်သွားပါတယ်။ ပြီးမှ statement တစ်ခုချင်း run ပါတယ်၊ နာမည် စာလုံးပေါင်းမှားတာ (ReferenceError) လို error တွေက အဲဒီ line ကို ရောက်မှ ပေါ်ပါတယ်။ Error က ဘယ်အဆင့်က လာလဲ သိရင် ဘယ်နေရာ ရှာရမလဲ သိပါတယ်။

Core concept: statement တွေ ထဲမှာ expression တွေ

Program တစ်ခုဆိုတာ statement တွေရဲ့ list တစ်ခုပါ။ Statement တစ်ခုက ညွှန်ကြားချက် တစ်ခုပါ — variable ကြေညာတာ၊ value assign လုပ်တာ၊ function ခေါ်တာ၊ ဆုံးဖြတ်ချက်ချတာ စတာတွေပါ။ Engine က statement တွေကို အပေါ်ကနေ အောက်ကို အစဉ်လိုက် run ပါတယ်။ Statement တစ်ခုကို semicolon (;) နဲ့ အဆုံးသတ်ပြီး line တစ်ကြောင်းမှာ statement တစ်ခုပဲ ရေးတာ ထုံးစံပါ။

Expression ဆိုတာ value တစ်ခု ထုတ်ပေးတဲ့ code အပိုင်းပါ — 5၊ x + y၊ "Hi " + name၊ x > 3 စတာတွေပါ။ Expression တွေက statement တွေထဲမှာ နေပါတယ်။ let z = x + y; မှာ x + y ဆိုတဲ့ expression ကို အရင်တွက်ပြီး ရလဒ်ကို statement က z ထဲ သိမ်းပါတယ်။ မှတ်ရလွယ်တဲ့ နည်းက — console.log(...) ရဲ့ ကွင်းထဲ ထည့်လို့ရရင် expression ပါ။

Curly brace { } တွေက statement တွေကို block တစ်ခုအဖြစ် စုပေးပြီး if၊ loop နဲ့ function တွေက သုံးပါတယ်။ Space နဲ့ indentation ကို engine က လျစ်လျူရှုပေမယ့် လူဖတ်ဖို့အတွက်တော့ မရှိမဖြစ်ပါ။ Identifier (နာမည်) တွေက case-sensitive ပါ — total၊ Total၊ TOTAL ဆိုတာ နာမည် သုံးခု ကွဲပါတယ်၊ keyword တွေကလည်း စာလုံးအသေး (Let မဟုတ်ဘဲ let) ပါ။ နာမည်မှာ စာလုံး၊ ဂဏန်း၊ _ နဲ့ $ ပါလို့ရပေမယ့် ဂဏန်းနဲ့ မစရပါဘူး၊ let၊ class၊ return လို reserved word တွေကိုလည်း နာမည်အဖြစ် မသုံးရပါဘူး။ JavaScript မှာ firstName လို camelCase ကို ထုံးစံအဖြစ် သုံးပါတယ်။

Comment ပုံစံ နှစ်မျိုး ရှိပါတယ် — // က line ရဲ့ ကျန်တဲ့အပိုင်းကို လျစ်လျူရှုစေပြီး /* ... */ က line အများကြီးကို ဖုံးနိုင်ပါတယ်။ Code က ဘာလုပ်လဲထက် ဘာကြောင့် လုပ်လဲကို ရှင်းပြဖို့ comment ကို သုံးပါ၊ debug လုပ်တုန်း line တစ်ကြောင်းကို ခဏ ပိတ်ထားဖို့လည်း သုံးလို့ရပါတယ်။ String တွေကို double quote၊ single quote ဒါမှမဟုတ် backtick နဲ့ ရေးလို့ရပါတယ်။ Project တစ်ခုမှာ quote စတိုင် တစ်မျိုးကို ရွေးပြီး စာသားထဲမှာ quote ပါနေရင် "It's fine" လို တခြား quote အမျိုးအစားကို သုံးပါ။

အပိုင်းဥပမာတာဝန်
Statementlet z = x + y;ညွှန်ကြားချက် တစ်ခု လုပ်တယ်
Expressionx + yValue တစ်ခု ထုတ်ပေးတယ်
Block{ ... }Statement တွေကို စုတယ်
Comment// note /* note */Engine က လျစ်လျူရှုတယ်
မှတ်ထားရမယ့် ပုံစံ: parse ပြီးမှ run
  1. Engine က file တစ်ခုလုံးကို parse လုပ်တယ်
  2. သဒ္ဒါမှားရင် ဘာမှ မ run ဘူး
  3. Statement တွေကို အပေါ်ကနေ အောက် run တယ်
  4. Expression ကို statement မတိုင်ခင် တွက်တယ်

SyntaxError = parse အဆင့်က error၊ ReferenceError / TypeError = run နေတုန်း error ပါ။

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

javascript
// a comment: ignored by the engine
let name = expression;       // statement containing an expression
name = name + otherValue;    // reassignment statement

{                            // a block groups statements
  statementOne;
  statementTwo;
}

= ရဲ့ ညာဘက်က expression ကို အရင်တွက်ပြီး ဘယ်ဘက်က နာမည်ထဲ ထည့်ပါတယ်။ Block ကို တစ်ခုတည်း သုံးတာ ရှားပြီး if၊ for၊ function တွေရဲ့ အစိတ်အပိုင်းအဖြစ် တွေ့ရများပါတယ်။

Example: statement လေးခု

ဒီ program မှာ statement လေးခုနဲ့ comment သုံးကြောင်း ပါပါတယ်။ Statement တစ်ခုချင်းစီမှာ expression ဘယ်နေရာလဲ ရှာကြည့်ပါ၊ ပြီးရင် x ကို 7 လို့ ပြောင်းပြီး output ကို ခန့်မှန်းကြည့်ပါ။

javascript
// Declaring variables
let x = 5;
let y = 10;

// An expression that calculates a value
let z = x + y;

// Output the result to the console
console.log("The value of z is: " + z);

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

  1. Line 1// Declaring variables

    // နဲ့ စတဲ့ line က comment ပါ။ Engine က လုံးဝ မဖတ်ဘဲ ကျော်သွားလို့ လူဖတ်ဖို့ မှတ်စု ရေးတဲ့နေရာပါ။

  2. Line 2let x = 5;

    let keyword နဲ့ x ဆိုတဲ့ variable ကို ကြေညာပြီး expression 5 ကို ထည့်ပါတယ်။ Semicolon က statement ပြီးကြောင်း ပြပါတယ်။

  3. Line 6let z = x + y;

    x + y က expression ပါ။ Engine က x (5) နဲ့ y (10) ကို ရှာပြီး 15 တွက်ကာ z ထဲ သိမ်းပါတယ်။ ဒီ line မှာ X + y လို့ ရေးမိရင် X is not defined ဆိုတဲ့ ReferenceError ဖြစ်ပါမယ်။

  4. Line 9console.log("The value of z is: " + z);

    Function call statement ပါ။ ကွင်းထဲက expression မှာ string နဲ့ number ကို + နဲ့ ဆက်ထားလို့ number 15 က စာသားအဖြစ် ပြောင်းပြီး "The value of z is: 15" ဖြစ်ပါတယ်။

You should see
The value of z is: 15

Semicolon နဲ့ automatic semicolon insertion

Semicolon မထည့်ခဲ့ရင် JavaScript က line break အများစုမှာ semicolon ကို အလိုအလျောက် ထည့်ပေးပါတယ် (automatic semicolon insertion, ASI)။ ဒါကြောင့် semicolon မပါတဲ့ code က များသောအားဖြင့် အလုပ်လုပ်ပါတယ်။ ဒါပေမယ့် trap တစ်ခု ရှိပါတယ် — return နောက်မှာ line ဆင်းလိုက်ရင် engine က return; လို့ ယူဆပြီး undefined ကို ပြန်ပါတယ်၊ နောက်က object ကို ဘယ်တော့မှ မရောက်ပါဘူး။

asi-trap.js
function getConfigBroken() {
  return
    { debug: true };
}

function getConfigFixed() {
  return { debug: true };
}

console.log(getConfigBroken()); // undefined
console.log(getConfigFixed());  // { debug: true }

Semicolon ထည့်တာ မထည့်တာ ဘယ်စတိုင်ကို ရွေးရွေး project တစ်ခုလုံးမှာ တစ်မျိုးတည်း သုံးပါ၊ return value ကိုတော့ return နဲ့ line တစ်ကြောင်းတည်းမှာ အမြဲ စပါ။

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

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

Team တွေက semicolon၊ quote အမျိုးအစား၊ indentation လို စတိုင်အသေးအဖွဲတွေအတွက် မငြင်းခုံရအောင် Prettier လို formatter နဲ့ ESLint လို linter ကို သုံးပါတယ်။ File ကို save လုပ်တာနဲ့ formatter က code ကို တစ်ပုံစံတည်း ပြန်စီပေးပြီး linter က မသုံးတဲ့ variable တွေ၊ မှားရေးထားတဲ့ နာမည်တွေကို run မလုပ်ခင်ကတည်းက သတိပေးပါတယ်။ ဒါကြောင့် code review မှာ syntax စတိုင်ထက် logic ကို အာရုံစိုက်လို့ ရပါတယ်။

Production မှာ ပို့တဲ့ JavaScript ကို minify လုပ်လေ့ရှိပါတယ် — space၊ comment နဲ့ line break တွေကို ဖယ်ပြီး နာမည်တွေကို တိုအောင် ပြောင်းပါတယ်။ Engine က အဲဒါတွေကို ဂရုမစိုက်ဘူးဆိုတာကို ဒါက သက်သေပြပါတယ်။ Error တစ်ခု ဖြစ်ရင် source map က minified code ကို မူရင်း file နဲ့ line number ဆီ ပြန်ညွှန်ပေးပါတယ်။ Syntax error message ကို မြန်မြန် ဖတ်တတ်တာကလည်း နေ့စဉ် skill တစ်ခုပါ — message မှာ file၊ line၊ column နဲ့ Unexpected token ဒါမှမဟုတ် missing ) after argument list လို အရိပ်အမြွက်တစ်ခု ပါလေ့ရှိပါတယ်။

အမှားများ

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

1. စာလုံးအကြီးအသေး မှားတာ

Console.log("hi") က ReferenceError: Console is not defined ဖြစ်ပြီး document.getElementByID(...) က TypeError: ... is not a function ဖြစ်ပါတယ်၊ မှန်တာက getElementById ပါ။ Editor ရဲ့ autocomplete ကို သုံးပါ၊ ဒါမှ စာလုံးပေါင်း ကိုယ်တိုင် မရိုက်ရပါဘူး။

2. Quote၊ bracket ဒါမှမဟုတ် brace မပိတ်တာ

ပိတ်မထားတဲ့ "၊ ( ဒါမှမဟုတ် { တစ်ခုက file တစ်ခုလုံးကို SyntaxError ဖြစ်စေပါတယ်၊ error က file ရဲ့ အဆုံးကို ညွှန်ပြနိုင်ပေမယ့် တကယ့် အမှားက အပေါ်ပိုင်းမှာ ရှိနေတတ်ပါတယ်။ Editor ရဲ့ bracket matching နဲ့ indentation ကို ကြည့်ပြီး အတွဲမညီတဲ့ နေရာကို ရှာပါ။

3. Reserved word ဒါမှမဟုတ် ဂဏန်းနဲ့ စတဲ့ နာမည် သုံးတာ

let class = "A"; နဲ့ let 2ndPlace = "Ko Ko"; နှစ်ခုလုံး SyntaxError ပါ။ selectedClass နဲ့ secondPlace လို ဖော်ပြချက်ပါတဲ့ နာမည်ကို ရွေးပါ။

Best Practices

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

  • Line တစ်ကြောင်းမှာ statement တစ်ခုပဲ ရေးပြီး တစ်ခုချင်းကို semicolon နဲ့ ပိတ်ပါ (ဒါမှမဟုတ် linter နဲ့အတူ ASI ကို တသမတ်တည်း အားကိုးပါ)။
  • Block တိုင်းကို indentation ပမာဏ တူတူ (JavaScript မှာ space 2 ခု အသုံးများ) နဲ့ ရေးပါ၊ ဒါမှ code ရဲ့ ပုံသဏ္ဌာန်က logic နဲ့ ကိုက်ပါမယ်။
  • နာမည်တွေကို စကားလုံးအပြည့်နဲ့ camelCase ပေးပါ — tp ထက် totalPrice က ပိုကောင်းပါတယ်။
  • Code ကို လက်နဲ့ ညှိမနေဘဲ save လုပ်တိုင်း formatter က layout ကို ပြင်ပေးပါစေ။

Mini challenge

Mini challenge: ဈေးဝယ် receipt

Statement တစ်ခုကို line တစ်ကြောင်းစီ ရေးပြီး ပစ္စည်းတစ်မျိုးအတွက် receipt အတိုလေး print ထုတ်တဲ့ program ရေးပါ။

  • itemName ("Notebook")၊ price (2.5) နဲ့ quantity (4) ကို variable တွေအဖြစ် ကြေညာရမယ်
  • subtotal = price နဲ့ quantity မြှောက်လဒ်၊ tax = subtotal ရဲ့ 10%၊ total = subtotal + tax
  • Item line၊ subtotal line၊ total line ဆိုပြီး သုံးကြောင်း print ထုတ်ရမယ်
  • Statement တိုင်း semicolon နဲ့ ဆုံးရမယ်
receipt.js
// TODO: declare itemName, price and quantity (one statement per line)

// TODO: subtotal = price times quantity

// TODO: tax = 10% of subtotal, total = subtotal plus tax

// TODO: print "Notebook x 4", "Subtotal: ..." and "Total: ..."

ရလဒ်: Line သုံးကြောင်း ထွက်ပါမယ်: Notebook x 4 Subtotal: 10 Total: 11

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

10% ကို 0.1 နဲ့ မြှောက်ပြီး တွက်ပါ။ String နဲ့ number ကို + နဲ့ ဆက်ရင် စာသား ရပါတယ်: "Total: " + total။

အဖြေ ကြည့်ရန်
receipt.js
let itemName = "Notebook";
let price = 2.5;
let quantity = 4;

let subtotal = price * quantity;
let tax = subtotal * 0.1;
let total = subtotal + tax;

console.log(itemName + " x " + quantity);
console.log("Subtotal: " + subtotal);
console.log("Total: " + total);

Quick Quiz 1 / 3

ဒီထဲက ဘယ်ဟာက statement မဟုတ်ဘဲ expression (value ထုတ်ပေးတဲ့ code) လဲ။

Quick Quiz 2 / 3

function f() { return 42; } ပြီးနောက် console.log(f()); က ဘာ print ထုတ်မလဲ။

Quick Quiz 3 / 3

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

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

  • Program က statement တွေရဲ့ list ဖြစ်ပြီး statement တွေထဲမှာ value ထုတ်ပေးတဲ့ expression တွေ ရှိပါတယ်။
  • { } က block ကို ဖွဲ့ပြီး // နဲ့ /* */ က comment ပါ၊ space နဲ့ indentation က လူဖတ်ဖို့ပါ။
  • JavaScript က case-sensitive ဖြစ်လို့ userName နဲ့ username က နာမည်နှစ်ခု ကွဲပါတယ်။
  • SyntaxError က file တစ်ခုလုံးကို မ run စေပါဘူး၊ return value ကို return နဲ့ line တစ်ကြောင်းတည်းမှာ ရေးပါ။

MDN — JavaScript Guide — MDN Web Docs

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

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

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

JS Syntax | Thuta Learning