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)
it throws an exception.
I don't understand this behavior, because if the first tells undefined
, then the second check should evaluate to if (undefined === undefined)
, why does it throw a ReferenceError exception?
typeof
looks like a function call, but it is not - it is an operator. Operators are allowed to break rules. typeof(qweasdasd)
does not assume qweasdasd
exists; whether it exists or not and what it is is what typeof
exists to discover. However, when you test qweasdasd === undefined
, you are using qweasdasd
as a value, and JS complains when you use a variable that you haven't assigned a value to.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With