Are these undefined checking identical in behavior?

前端 未结 2 555
孤城傲影
孤城傲影 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:27

    if (something === undefined) is the standard normal way

    typeof something === 'undefined' on a declared variable is mostly an overdefensive solution dating from the time where you could change window.undefined. If you don't know if your variable is declared, it has the advantage of not raising an error but I don't think a legit code should support the case of a variable whose declarative state is unknown.

    void 0 (or void anything) is a normalized way to get undefined so it's equivalent to the first one but useless.

提交回复
热议问题