JavaScript check if value is only undefined, null or false

后端 未结 9 2066
暗喜
暗喜 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:26

    Another solution:

    Based on the document, Boolean object will return true if the value is not 0, undefined, null, etc. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean

    If value is omitted or is 0, -0, null, false, NaN, undefined, or the empty string (""), the object has an initial value of false.

    So

    if(Boolean(val))
    {
    //executable...
    }

提交回复
热议问题