ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
Data type ဆိုတာ value တစ်ခုက ဘယ်လို အမျိုးအစားလဲဆိုတာပါ — စာသားလား၊ ဂဏန်းလား၊ မှန်/မှား လား၊ ဘာမှ မရှိတာလား။ အမျိုးအစားက အဲဒီ value နဲ့ ဘာလုပ်လို့ရလဲဆိုတာကို ဆုံးဖြတ်ပါတယ်။ ဂဏန်းနှစ်ခုကို ပေါင်းလို့ရပြီး စာသားကိုတော့ စာလုံးအကြီး ပြောင်းလို့ရပါတယ်။
JavaScript မှာ type ကို value ကိုယ်တိုင်က သယ်ဆောင်ပြီး variable က မသယ်ပါဘူး။ ဒါကြောင့် variable တစ်ခုထဲမှာ အရင် number ထည့်ပြီး နောက်မှ string ထည့်လို့ ရပါတယ်။ ဒါက လွတ်လပ်မှုပေးပေမယ့် "5" (စာသား) နဲ့ 5 (ဂဏန်း) လို ပုံတူပြီး အမျိုးအစားကွဲတဲ့ value တွေကို ရောမိရင် bug ဖြစ်တတ်ပါတယ်။
Problem → Why → Solution
ပြဿနာ: 2 + 1 က 21 ဖြစ်သွားတယ်
Shop page တစ်ခုမှာ user က quantity box ထဲ 2 လို့ ရိုက်ပါတယ်။ Code က ပစ္စည်း တစ်ခု အပို ထည့်ပေးဖို့ 1 ပေါင်းလိုက်တော့ cart မှာ 3 မဟုတ်ဘဲ 21 ဖြစ်သွားပါတယ်။ Error message တစ်ခုမှ မပေါ်ဘဲ ဖောက်သည်ဆီ ပစ္စည်း ၂၁ ခုအတွက် bill ရောက်သွားနိုင်ပါတယ်။
// values read from a form field are always strings
const quantity = "2";
const extra = 1;
console.log(quantity + extra); // "21", not 3
console.log(typeof quantity); // "string" - the clueဘာကြောင့် 21 ဖြစ်တာလဲ
+ operator က တစ်ဖက်ဖက်က string ဖြစ်နေရင် ပေါင်းတာ မဟုတ်ဘဲ စာသား ဆက်ပါတယ်။ HTML input၊ URL နဲ့ prompt() ကနေ ရတဲ့ value တွေက ဂဏန်းနဲ့ တူနေရင်တောင် string ပါ။ JavaScript က type မကိုက်ညီတာကို သတိမပေးပါဘူး၊ ဒါကြောင့် typeof နဲ့ value ရဲ့ type ကို စစ်ပြီး တွက်ချက်မှု မလုပ်ခင် Number() နဲ့ ပြောင်းဖို့ ကိုယ်တိုင် သိထားရပါမယ်။
Core concept: primitive ခုနစ်မျိုးနဲ့ object
JavaScript က dynamically typed ဖြစ်ပါတယ် — type က value တွေမှာ ရှိပြီး variable တွေမှာ မရှိပါဘူး။ Value တွေကို အုပ်စုကြီး နှစ်ခု ခွဲပါတယ်။ Primitive တွေက ရိုးရှင်းပြီး ပြောင်းလဲလို့ မရတဲ့ value တွေပါ၊ value ချင်း နှိုင်းယှဉ်ပါတယ်။ ကျန်တာအားလုံး — plain object၊ array၊ function၊ date — က object တွေဖြစ်ပြီး reference နဲ့ သိမ်းပါတယ်၊ ဒါကြောင့် content တူတဲ့ object နှစ်ခုက === မဖြစ်ပါဘူး။ Dynamic ဖြစ်တာကို တိုက်ရိုက် မြင်နိုင်ပါတယ် — let value = 5; value = "five"; က လုံးဝ မှန်ကန်ပြီး typeof value က "number" ကနေ "string" ဆီ ပြောင်းသွားပါတယ်။ Primitive တွေကို ဂရုတစိုက် ရေတွက်ကြည့်ပါ — bigint ပါ ထည့်ရင် ခုနစ်မျိုး (string, number, bigint, boolean, undefined, null, symbol) ရှိပါတယ်။
| Type | ဥပမာ | မှတ်ချက် |
|---|---|---|
| string | "Hello", 'Hi', `Hey` | စာသား |
| number | 100, 3.14, -7, NaN | Integer ရော decimal ရော |
| bigint | 9007199254740993n | အရွယ်မကန့်သတ် ကိန်းပြည့် |
| boolean | true, false | Yes/no တန်ဖိုး |
| undefined | let car; | Value မထည့်ရသေး |
| null | let middleName = null; | တမင် ဗလာ ထားတာ |
| symbol | Symbol("id") | ထပ်မတူတဲ့ key |
| object | { name: "Mya" }, [1, 2] | Primitive မဟုတ်တာ အားလုံး |
typeof operator က value ရဲ့ type ကို "number"၊ "string" လို string တစ်ခုအဖြစ် ပြန်ပေးပါတယ်။ ထူးဆန်းချက် နှစ်ခု မှတ်ထားပါ။ typeof null က "object" ပြန်ပါတယ် — JavaScript ရဲ့ ပထမဆုံး version ကတည်းက ပါလာတဲ့ bug ဖြစ်ပြီး web site ဟောင်းတွေ မပျက်အောင် မပြင်ဘဲ ထားခဲ့တာပါ။ Array တွေကလည်း "object" ပြန်ပြီး function တွေကတော့ "function" ပြန်ပါတယ်။ ဒါကြောင့် null ကို x === null နဲ့၊ array ကို Array.isArray(x) နဲ့ စစ်ပါ။
undefined နဲ့ null နှစ်ခုလုံး "value မရှိဘူး" လို့ ဆိုလိုပေမယ့် ဘယ်သူက သတ်မှတ်လဲ ကွာပါတယ်။ undefined က JavaScript က ပေးတာပါ — value မထည့်ရသေးတဲ့ variable၊ မရှိတဲ့ object property စတာတွေပါ။ null ကတော့ programmer က "ဒီမှာ တမင် ဘာမှ မရှိဘူး" လို့ ရွေးထည့်တာပါ။ Number type တစ်ခုတည်းက integer နဲ့ decimal နှစ်မျိုးလုံးကို 64-bit floating point အဖြစ် သိမ်းလို့ 0.1 + 0.2 က 0.30000000000000004 ဖြစ်ပြီး ကိန်းပြည့်တွေကို Number.MAX_SAFE_INTEGER (9007199254740991) အထိပဲ တိကျစွာ ကိုင်နိုင်ပါတယ်၊ ဒီထက်ကြီးရင် bigint သုံးပါ။
- Type က value မှာ ရှိတယ်၊ variable မှာ မဟုတ်
- Primitive ခုနစ်မျိုး၊ ကျန်တာ object
- typeof က type ကို string နဲ့ ပြန်ပေး
- Math မလုပ်ခင် string ကို ပြောင်း
typeof null === "object" ကို မယုံပါနဲ့ — null ကို === null နဲ့ပဲ စစ်ပါ။
အထွေထွေ ပုံစံ
typeof value // "string", "number", "bigint", "boolean",
// "undefined", "symbol", "object" or "function"
value === null // the only reliable null check
Array.isArray(value) // the reliable array check
Number(text) // string -> number (NaN if it is not numeric)
String(value) // anything -> string
Boolean(value) // anything -> true or falsetypeof က ရလဒ် ရှစ်မျိုးထဲက တစ်ခုကို string အဖြစ် ပြန်ပေးပါတယ်။ Number()၊ String()၊ Boolean() ကို new မပါဘဲ ခေါ်ရင် value ကို အဲဒီ primitive type ဆီ ပြောင်းပေးပါတယ်။
Example: type တိုင်းကို typeof နဲ့ စစ်ကြည့်ခြင်း
ဒီ example မှာ bigint၊ symbol နဲ့ null အပါအဝင် type အားလုံး ပါပြီး console.log တစ်ခုစီက ရလဒ်တွေကို space ခြားပြီး line တစ်ကြောင်းစီ ထုတ်ပါတယ်။ နောက်ဆုံး line နှစ်ကြောင်းကို run မလုပ်ခင် ခန့်မှန်းကြည့်ပါ။
let length = 16; // number
let color = "Yellow"; // string
let isDone = true; // boolean
let car; // undefined: declared, no value yet
let middleName = null; // null: deliberately empty
let population = 9007199254740993n; // bigint: whole numbers of any size
let id = Symbol("id"); // symbol: a unique key
let person = { firstName: "John", lastName: "Doe" }; // object
console.log(typeof length, typeof color, typeof isDone);
console.log(typeof car, typeof population, typeof id);
console.log(typeof person, typeof [1, 2]);
console.log(typeof middleName, middleName === null);Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Line 4
let car;Value မပေးဘဲ ကြေညာထားလို့ JavaScript က car ကို undefined အဖြစ် အလိုအလျောက် ထားပါတယ်။
- Line 6
let population = 9007199254740993n;အဆုံးက n က bigint ဖြစ်ကြောင်း ပြပါတယ်။ n မပါရင် ဒီဂဏန်းက safe integer အကန့်အသတ်ကို ကျော်လို့ 9007199254740992 လို့ အနီးစပ်ဆုံးကို ပြောင်းသွားပါမယ်။
- Line 12
console.log(typeof person, typeof [1, 2]);Object နဲ့ array နှစ်ခုလုံး "object" ပြန်ပါတယ်။ typeof တစ်ခုတည်းနဲ့ array ကို ခွဲလို့ မရဘူးဆိုတာကို ဒီ line က ပြပါတယ်။
- Line 13
console.log(typeof middleName, middleName === null);typeof null က "object" ပြန်တဲ့ ထူးဆန်းချက်ကို ပြပြီး === null က true ပြန်လို့ null ကို စစ်ဖို့ မှန်ကန်တဲ့ နည်းကိုလည်း ပြပါတယ်။
number string boolean
undefined bigint symbol
object object
object trueType conversion: string ကနေ number
+ က string ကို တွေ့ရင် စာသား ဆက်ပေမယ့် *၊ -၊ / တို့ကတော့ string ကို number အဖြစ် အလိုအလျောက် ပြောင်းပါတယ်။ ဒီလို အလိုအလျောက် ပြောင်းတာ (coercion) ကို အားကိုးမယ့်အစား Number() နဲ့ ကိုယ်တိုင် ပြောင်းပါ၊ ပြီးရင် ဂဏန်းမဟုတ်တဲ့ input ကြောင့် ရတဲ့ NaN ကို Number.isNaN() နဲ့ စစ်ပါ။
const fromInput = "5";
console.log(fromInput + 1); // 51: + joins strings
console.log(fromInput * 2); // 10: * converts to a number
console.log(Number(fromInput) + 1); // 6: convert first, then add
console.log(Number("abc")); // NaN: not a number
console.log(Number.isNaN(Number("abc"))); // true
console.log(0.1 + 0.2); // 0.30000000000000004NaN က typeof အရ "number" ဖြစ်နေတာနဲ့ NaN === NaN က false ဖြစ်တာ ထူးဆန်းပေမယ့် မှန်ပါတယ်၊ ဒါကြောင့် === NaN နဲ့ စစ်လို့ မရဘဲ Number.isNaN() ကိုပဲ သုံးရတာပါ။ Number() က string တစ်ခုလုံးကိုလည်း တင်းကျပ်စွာ စစ်ပါတယ် — Number("12px") က NaN ဖြစ်ပြီး parseInt("12px", 10) ကတော့ ရှေ့က ဂဏန်းတွေကို ဖတ်ပြီး 12 ကို ပြန်ပေးပါတယ်။ Input အလွတ်ကိုလည်း သတိထားပါ၊ Number("") က NaN မဟုတ်ဘဲ 0 ဖြစ်လို့ပါ။
လက်တွေ့ project တွေမှာ
Real project တွေမှာ ဘယ်လို သုံးလဲ
API တွေကနေ ရတဲ့ JSON data မှာ string၊ number၊ boolean၊ null၊ array နဲ့ object တွေပဲ ပါပါတယ် — undefined၊ bigint နဲ့ symbol ကို JSON က မသိပါဘူး။ API တွေက "ဒီ field မှာ value မရှိဘူး" ကို null နဲ့ ပြလေ့ရှိလို့ user.phone === null လို စစ်ချက်တွေ နေရာတိုင်းမှာ တွေ့ရပါတယ်။ ID ကြီးကြီးတွေကို string အဖြစ် ပို့လေ့ရှိတာကလည်း number ရဲ့ safe integer အကန့်အသတ်ကြောင့်ပါ။
ငွေကြေး တွက်ချက်တဲ့ app တွေက 0.1 + 0.2 ပြဿနာကို ရှောင်ဖို့ ဈေးနှုန်းကို ဒေါ်လာ 19.99 အစား cent 1999 လို ကိန်းပြည့်အဖြစ် သိမ်းပြီး ပြတဲ့အခါမှ ပြန်ပြောင်းပါတယ်။ Form ကနေ ဝင်လာတဲ့ value တိုင်းကိုလည်း server ဆီ မပို့ခင် Number() နဲ့ ပြောင်းပြီး NaN ဟုတ်မဟုတ် စစ်ကြပါတယ်။ Zod လို runtime validation library တွေ ရှိရတဲ့ အဓိက အကြောင်းကလည်း အဲဒီ boundary မှာ မေးခွန်းတစ်ခုကို ဖြေဖို့ပါ — ဒီ value က ကျန်တဲ့ code မျှော်လင့်ထားတဲ့ type အစစ် ဟုတ်ရဲ့လား။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. null ကို typeof နဲ့ စစ်တာ
if (typeof data === "object") က data null ဖြစ်နေရင်လည်း true ဖြစ်ပြီး နောက် line မှာ data.name ကို ဖတ်တော့ TypeError: Cannot read properties of null ဖြစ်ပါတယ်။ data !== null ကို အရင် စစ်ပါ။
2. Input string တွေနဲ့ တိုက်ရိုက် တွက်တာ
Form field နှစ်ခုကို a + b လို့ ပေါင်းရင် "10" + "5" = "105" ရပါတယ်။ Number(a) + Number(b) လို့ အရင် ပြောင်းပြီး Number.isNaN() နဲ့ မမှန်တဲ့ input ကို ဖမ်းပါ။
3. Decimal တွေကို === နဲ့ တိုက်ရိုက် နှိုင်းယှဉ်တာ
0.1 + 0.2 === 0.3 က false ပါ။ ငွေကြေးအတွက် cent လို ကိန်းပြည့်နဲ့ တွက်ပါ၊ ဒါမှမဟုတ် ပြတဲ့အခါ toFixed(2) နဲ့ round လုပ်ပါ။
4. typeof နဲ့ array ကို စစ်တာ
typeof [] က "array" မဟုတ်ဘဲ "object" ပါ၊ ဒါကြောင့် typeof x === "array" က ဘယ်တော့မှ true မဖြစ်ပါဘူး။ Array.isArray(x) ကို သုံးပါ။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- Program ထဲကို data ဝင်လာတဲ့ နေရာ (form၊ URL၊ API) မှာပဲ type ပြောင်းပြီး စစ်ပါ၊ ကျန်တဲ့ code က type မှန်ပြီးသား value တွေနဲ့ပဲ အလုပ်လုပ်ပါစေ။
- "ဘာမှ မရှိဘူး" လို့ ကိုယ်တိုင် ပြချင်ရင် undefined အစား null ကို သုံးပါ။
- Variable တစ်ခုကို type တစ်မျိုးတည်းနဲ့ပဲ သုံးပါ — အရင် number၊ နောက်မှ string ဖြစ်တာက ဖတ်ရခက်ပါတယ်။
Mini challenge
Mini challenge: form data ကို စစ်ဆေးခြင်း
Form ကနေ ရောက်လာတဲ့ value တွေရဲ့ type ကို စစ်၊ string math bug ကို ပြ၊ ပြီးရင် number ပြောင်းပြီး မှန်ကန်စွာ တွက်ပါ။
- rawAge နဲ့ rawQuantity ရဲ့ typeof ကို line တစ်ကြောင်းတည်းမှာ print ထုတ်ရမယ်
- rawAge + 1 ရဲ့ မှားတဲ့ ရလဒ်ကို "String math:" နဲ့ ပြရမယ်
- Number() နဲ့ ပြောင်းပြီး type အသစ်တွေနဲ့ "Number math:" ကို ပြရမယ်
- "Total items:" (quantity * 2) နဲ့ coupon !== null ကို သုံးပြီး "Has coupon:" ကို print ထုတ်ရမယ်
const rawAge = "25"; // values from a form arrive as strings
const rawQuantity = "3";
const coupon = null; // the user left the coupon field empty
// TODO: print the types of rawAge and rawQuantity on one line
// TODO: print "String math:" followed by rawAge + 1
// TODO: convert both raw values to numbers
// TODO: print their types again, then "Number math:" with age + 1
// TODO: print "Total items:" (quantity * 2) and "Has coupon:" (true/false)ရလဒ်: Line ခြောက်ကြောင်း ထွက်ပါမယ်: string string String math: 251 number number Number math: 26 Total items: 6 Has coupon: false
အရိပ်အမြွက် ကြည့်ရန်
console.log("Has coupon:", coupon !== null) လို argument နှစ်ခု ပေးရင် space တစ်ခု ခြားပြီး ထွက်ပါတယ်။ typeof coupon ကိုတော့ မသုံးပါနဲ့ — "object" ပဲ ရမှာပါ။
အဖြေ ကြည့်ရန်
const rawAge = "25";
const rawQuantity = "3";
const coupon = null;
console.log(typeof rawAge, typeof rawQuantity);
console.log("String math:", rawAge + 1);
const age = Number(rawAge);
const quantity = Number(rawQuantity);
console.log(typeof age, typeof quantity);
console.log("Number math:", age + 1);
console.log("Total items:", quantity * 2);
console.log("Has coupon:", coupon !== null);Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Primitive ခုနစ်မျိုး ရှိပါတယ်: string, number, bigint, boolean, undefined, null, symbol၊ ကျန်တာအားလုံး object ပါ။
- typeof က type ကို string နဲ့ ပြန်ပေးပေမယ့် null နဲ့ array အတွက် "object" ပြလို့ === null နဲ့ Array.isArray() ကို သုံးပါ။
- undefined က value မထည့်ရသေးတာ၊ null က တမင် ဗလာ ထားတာပါ။
- Form ကနေ ရတဲ့ value တွေက string ဖြစ်လို့ Number() နဲ့ ပြောင်းပြီး Number.isNaN() နဲ့ စစ်ပါ။
MDN — JavaScript Guide — MDN Web Docs