how to check falsy with undefined or null?

前端 未结 4 1372
梦谈多话
梦谈多话 2021-01-19 03:32

undefined and null are falsy in javascript but,

var n = null;
if(n===false){
console.log(\'null\');
} else{
console.log(\'has value\');
}

b

4条回答
  •  隐瞒了意图╮
    2021-01-19 04:09

    === checks for identity - the exact same type and value. So null !== false firstly because they are not the same type, thus will not match when using ===.

    If you just want to check for any falsey value, then check with:

    if (!n)
    

    If you want to specifically check for null, then check for null like this:

    if (n === null)
    

提交回复
热议问题