Are these undefined checking identical in behavior?

前端 未结 2 556
孤城傲影
孤城傲影 2021-01-28 12:01

I\'ve seem different approaches for (strict equality) checking for undefined:

  • if (something === undefined)
  • if (typeof some
2条回答
  •  清歌不尽
    2021-01-28 12:23

    1. Literally undefined
    2. Test for existence, as in "variable not declared".
    3. Same as undefined. Older version of the JS standard let you change the value of undefined as it's just a variable. void 0 is undefined, it's safer.

    An extra one:

    1. if (x == null). Tests for undefined and null because undefined == null but remember, undefined !== null

    In JavaScript there's a type 'undefined' and a value undefined. The value undefined is of type 'undefined'.

提交回复
热议问题