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?
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?
- Cannot reproduce: jsfiddle/y1xhw9un – Jamiec Commented Jul 28, 2015 at 9:09
- @Jamiec — Can reproduce: jsfiddle/y1xhw9un/1 – Quentin Commented Jul 28, 2015 at 9:10
- @Quentin - ahh, thats undeclared, When it is undefined, then it works jsfiddle/y1xhw9un/2 – Jamiec Commented Jul 28, 2015 at 9:11
-
1
@Jamiec: In that snippet, you covertly defined
y
to have valueundefined
. OP is asking about a case when the variable is undefined, not when it has valueundefined
. "undeclared" does not exist as a term in JavaScript; the error message is "ReferenceError: ... is not defined". – Amadan Commented Jul 28, 2015 at 9:11 -
@Amadan - No, it appears (from Quentins sample) the OP has an undeclared variable - that's quite different to undefined. I think your answer is right thogh -
typeof
is letting you "get away with it" where the equality check isnt. – Jamiec Commented Jul 28, 2015 at 9:12
6 Answers
Reset to default 7typeof
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 plains when you use a variable that you haven't assigned a value to.
Trying to read the value of an undeclared variable (which you have to do before you can pare that value to the value of undefined
) throws a ReferenceError.
Applying the typeof
operator to an undeclared variable does not.
I think I have a very simple explanation for this -- because the specs say so:
typeof
operator is not supposed to throw the ReferenceError exception if the variable is not defined
- If Type(val) is Reference, then
a. If IsUnresolvableReference(val) is true, return"undefined"
.
===
operator is supposed to throw the ReferenceError exception if the operand(s) refer to undefined variables
- Let lval be GetValue(lref).
[And inside the definition of GetValue we have]
- Let base be the result of calling GetBase(V).
- If IsUnresolvableReference(V), throw a ReferenceError exception.
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
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.
typeof
is an operator that returns you a string indicating the type of the unevaluated operand. It doesn't matter if the unevaluated operand is undefined or not.
https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Operators/typeof
>>> if (a !== 'undefined');
Exception: ReferenceError: Can't find variable: a
>>> if (typeof a !== undefined);
undefined
>>> if (typeof a !== void 0);
undefined
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744135321a4560030.html
评论列表(0条)