JavaScript check if value is only undefined, null or false

后端 未结 9 2063
暗喜
暗喜 2021-01-31 08:09

Other than creating a function, is there a shorter way to check if a value is undefined,null or false only in JavaScript?

9条回答
  •  梦谈多话
    2021-01-31 08:24

    One way to do it is like that:

    var acceptable = {"undefined": 1, "boolean": 1, "object": 1};
    
    if(!val && acceptable[typeof val]){
      // ...
    }
    

    I think it minimizes the number of operations given your restrictions making the check fast.

提交回复
热议问题