How can I determine if a variable is 'undefined' or 'null'?

前端 未结 30 1524
耶瑟儿~
耶瑟儿~ 2020-11-22 03:30

How do I determine if variable is undefined or null?

My code is as follows:

var EmpN         


        
30条回答
  •  梦毁少年i
    2020-11-22 04:17

    With the solution below:

    const getType = (val) => typeof val === 'undefined' || !val ? null : typeof val;
    const isDeepEqual = (a, b) => getType(a) === getType(b);
    
    console.log(isDeepEqual(1, 1)); // true
    console.log(isDeepEqual(null, null)); // true
    console.log(isDeepEqual([], [])); // true
    console.log(isDeepEqual(1, "1")); // false
    etc...
    

    I'm able to check for the following:

    • null
    • undefined
    • NaN
    • empty
    • string ("")
    • 0
    • false

提交回复
热议问题