javascript - Using typeof vs === to check undeclared variable produces different result - Stack Overflow

If I have an undeclared variable and use typeof it tells me it's undefined.But if I then check it

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?

Share Improve this question edited Feb 4, 2018 at 19:44 Salman Arshad 273k84 gold badges444 silver badges534 bronze badges asked Jul 28, 2015 at 9:07 Konstantin MilyutinKonstantin Milyutin 12.4k12 gold badges61 silver badges86 bronze badges 5
  • 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 value undefined. OP is asking about a case when the variable is undefined, not when it has value undefined. "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
Add a ment  | 

6 Answers 6

Reset to default 7

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 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
  1. 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
  1. Let lval be GetValue(lref).

[And inside the definition of GetValue we have]

  1. Let base be the result of calling GetBase(V).
  2. 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信