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

前端 未结 6 961
闹比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:51

    You have an undefined variable, but not an undefined object value.

    console.log(variable) // undefined
    console.log(typeof variable) // "undefined"
    console.log(variable === undefined) // Reference error
    variable = undefined ; 
    console.log(variable === undefined) // true
    

提交回复
热议问题