Using typeof vs === to check undeclared variable produces different result

前端 未结 6 960
闹比i
闹比i 2020-12-20 19:39

If I have an undeclared variable and use typeof it tells me it\'s undefined. But if I then check it using if (qweasdasd === undefined)

6条回答
  •  抹茶落季
    2020-12-20 20:00

    Trying to read the value of an undeclared variable (which you have to do before you can compare that value to the value of undefined) throws a ReferenceError.

    Applying the typeof operator to an undeclared variable does not.

提交回复
热议问题