null နှင့် undefined JavaScript တို့၏ကွာခြားချက်

ကိုးကားစရာ

JavaScript

null ပုံမှန်အသုံးပြုမှု

function parameter တစ်ခုအနေနှင့် function parameter သည် object မဟုတ်ကြောင်းဆိုလိုသည်

            function myFn(obj) {
    if(null == obj) {
        // code
    }
}
        

အရာဝတ္ထုရှေ့ပြေးပုံစံကွင်းဆက်၏အဆုံးအဖြစ်

            Object.getPrototypeOf(Object.prototype);
        

variable ကိုလွှတ်သောအခါ, variable ကိုမှ null assign

            let o={
 code:'abc'
};
o = null;
        

undefined ၏ပုံမှန်အသုံးပြုမှု

ဒီ variable ကိုကြေငြာပေမယ့်မပေးပါဘူး၊ variable ရဲ့တန်ဖိုးကိုတော့မသတ်မှတ်ပါဘူး

            let o;
console.log(o);    //undefined
        

ခေါ်ဆိုသောအခါ function ကိုပေးသင့်သော parameter သည်ပေးမထားဘူး, ထို့နောက်ပေးထားသောမဟုတ်သော parameter သည်၏တန်ဖိုး undefined ဖြစ်ပါတယ်

            function myFn(o) {
    console.log(o);
}
myFn();   //undefined
        

အရာဝတ္ထုမပါ ၀ င်သော Properties များ၊ တန်ဖိုးသည် undefined ဖြစ်သည်

            let o={
 code:'abc'
};
console.log(o.name);    //undefined
        

function က value ကို return မလုပ်ရင် undefined ကိုပုံမှန်ပြန်သွားတယ်

            function myFn(obj) {
    obj = 'a';
}
console.log(myFn());    //undefined
        

null နှင့် undefined အကြားတူညီသည်

boolean သို့ကူးပြောင်းသည့်အခါအယူမှား

            console.log(Boolean(null));  // false
console.log(Boolean(undefined)); // false
        

null နှင့် undefined အကြားခြားနားချက်

နံပါတ်သို့ပြောင်းလဲသောအခါရလဒ်မှာကွဲပြားခြားနားသည်။ null ပြောင်းလဲမှု၏ရလဒ်သည်တရားဝင်နံပါတ်တန်ဖိုးဖြစ်သည့်အပြင် undefined ပြောင်းလဲခြင်း၏ရလဒ်သည် NaN ဖြစ်သည်။

            console.log(Number(null));  // 0
console.log(Number(undefined)); // NaN
        

အမျိုးအစားနှစ်မျိုးကွဲပြားသည်။ null အမျိုးအစားသည်အရာဝတ္ထုဖြစ်သည်။ undefined အမျိုးအစားမှာမသတ်မှတ်ပါ။

            console.log(typeof null); //object
console.log(typeof undefined); //undefined
        

null နှင့် undefined နှိုင်းယှဉ်

null နှင့် undefined တို့ကိုခွဲခြားရန် congruence ကိုသာသုံးနိုင်သည်။

            console.log(Boolean(null == undefined));  // true
console.log(Boolean(null === undefined)); // false
        

အကျဥ်းရုံးသည်

အထက်ပါနှိုင်းယှဉ်ချက်ပြီးနောက်ကျွန်ုပ်တို့သည်ဤအမျိုးအစားနှစ်မျိုးကိုရှင်းလင်းစွာနားလည်ထားသည်။ ပုံမှန်အားဖြင့်ကျွန်ုပ်တို့သည် variable တစ်ခုကိုကုဒ်ရေးသည့်အခါမှန်ကန်သောတန်ဖိုးရှိမရှိဆုံးဖြတ်သည့်အခါ၎င်းနှစ်ခုကိုခွဲခြားရန်မလိုအပ်ပါ။ အိမ်ခြံမြေတည်ရှိမရှိဆုံးဖြတ်ရန်ကျွန်ုပ်တို့လိုအပ်သောအခါကျွန်ုပ်တို့သည်သတ်မှတ်ထားသောအမျိုးအစားကိုတိကျသေချာစွာမဆုံးဖြတ်ရပါ။ နောက်ဆုံးအနေဖြင့်ကျွန်ုပ်တို့သည်ဂဏန်းတန်ဖိုးကိုလွန်သွားပါကကျွန်ုပ်တို့သည်ဆုံးဖြတ်ချက်ကို ဦး စွာဆုံးဖြတ်ရမည်။ တိုက်ရိုက်ဂဏန်းအမျိုးအစားသို့တိုက်ရိုက်မပို့နိုင်ပါ။