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

前端 未结 6 970
闹比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 19:59

    Trying to access undefined variable in Javascript gives ReferenceError. typeof is working because it not accessing variable value. Its checking its type.

    typeof not requires defined variable. Its parameter should be an expression representing the object or primitive whose type is to be returned.

提交回复
热议问题