0

Possible Duplicates:
Javascript: undefined !== undefined?
What is the best way to compare a value against 'undefined'?

I've played around with the console and got some strange results when checking undefined,
when I do var a; a's type and value become "undefined" right? So why a===undefined is true and a=="undefined" or a==="undefined" are false?
and, would typeof a == "undefined" be the best practice like in other languages?

Unrelated - how do I markup code in a question from iPhone?

Community
  • 1
  • 1
ilyo
  • 35,851
  • 46
  • 106
  • 159

4 Answers4

2

=== means compare type and value in Javascript. So

0 == '0' // true, because it is essentially toStringing both values
0 === '0' // false, because one is a Number and one is a String

When you check for a == "undefined" You are seeing if a is equal to the String value "undefined". undefined without quotes in Javascript is an undefined value. a === undefined compares a to the value undefined, and a === "undefined" compares a to the string "undefined".

Using a === undefined is a good practice for checking for definition

edit: this answer has some flaws, which I leave to the commenters to correct me

Andy Ray
  • 30,372
  • 14
  • 101
  • 138
  • You learn something new every day – Andy Ray Aug 25 '11 at 04:36
  • 1
    `typeof a === "undefined"` is good practice. `a === undefined` (or `a == undefined`) only works as long as there is no code assigning `undefined` equal to some defined value. (Admittedly 99.9% of the time nobody would assign a value to `undefined`, but JavaScript allows it. And having said that there are steps you can take to let you keep saying `undefined` directly even when using external libraries that may have redefined it.) – nnnnnn Aug 25 '11 at 04:41
  • Using *typeof* also helps prevent inadvertantly writing `a = undefined`, which might be a hard to find bug. – RobG Aug 25 '11 at 04:59
  • @nnnnnn, fortunately ECMAScript 5 finally makes the `undefined` property read-only. @Andy, in your first example (`0 == '0'`) the two values are not *toStringed*, if one of the operands is either a number or a boolean, *both* are converted to Number behind the scenes, by the algorithm that describes the rules of the equals operator, e.g. `'0' == false; // true`. Cheers. – Christian C. Salvadó Aug 25 '11 at 05:00
2

When doing a=="undefined" or a==="undefined" you're comparing the value of a with a string which contains the characters u, n, d, e, f, i, n, e, d.

So your expression boils down to undefined=="somestring", which is obviously false.

typeof returns a string, so in this case comparing it to a string works.

Sergey
  • 11,892
  • 2
  • 41
  • 52
  • so what is the value of an undefined variable? Isn't it "undefined"? – ilyo Aug 25 '11 at 05:15
  • 3
    No. The value of an undefined variable is undefined, but it is not "undefined" :) It's the same as if a = 0 then we can say that the value of a is zero, but it is not "ZERO" or "NOTHING". JavaScript complicates the matters a bit by attempting to convert variables from one type to another, so 0 == "0", but that is just a result of the conversion because typeof 0 == "number" and typeof "0" === "string", so 0 !== "0". My point is - unlike the example above, JavaScript does not attempt to convert a string "undefined" to undefined value, so undefined != "undefined". – Sergey Aug 26 '11 at 00:16
2

I suppose the best way is to perform strict equation check like a === undefined while typeof a == 'undefined' is overkill since there are no (at least as I know) situation which can lead to evaluating a === undefined to false while a is actually have a value of undefined.

I think comparsion of strings and taking typeof from variable is much slower than a strict equation (possibly speed tests needed).

Considering situation expression a itself is suitable way to check a for undefined value except for cases in which you need to handle false value of variable.

Eskat0n
  • 937
  • 9
  • 21
  • 1
    `typeof` may seem like it is overkill, but it is 100% reliable for this purpose while `a === undefined` is not. There's nothing stopping you saying `undefined = "defined";` giving `undefined` a value and causing all `something === undefined` tests to go wrong. (In practice of course it would be stupid to give `undefined` a value, but it can happen.) – nnnnnn Aug 25 '11 at 04:58
  • With 'use strict'; an undefined variable throw an error... – molokoloco Feb 05 '15 at 13:42
1

Just to cover one point: The word "undefined" is not special in javascript. There is no keyword or global representing it.

So when you do a === undefined it returns true because neither name has any value assigned to it - if you had somewhere previously created and assigned a variable with that name (like undefined = 1) then that statement would be false.

Paul Phillips
  • 6,093
  • 24
  • 34