开发者

What is the difference between "undefined" and undefined?

I'm trying this quiz in the Chrome console: Quiz

I can explain most of them somewhat after trying them out. But one thing confuses me:

var x = [typeof x, typeof y][1];
    typeof typeof x;

.... returns "string", which doesn't make any sense to me.

var x = [typeof x, typeof y][1]; 

returns "undefined"

typeof "undefined"

returns "string", which makes some sense because undefined was in quotes. But overall, I don't see the pur开发者_运维技巧pose of "undefined" in coexistance with undefined. Also, what kind of array syntax is that? "Javascript The Good Parts" says that there are no multidimensional arrays.


  1. undefined is actually window.undefined in most situations. It's just a variable.
  2. window.undefined happens to not be defined, unless someone defines it (try undefined = 1 and typeof undefined will be "number").
  3. typeof is an operator that always returns a string, describing the type of a value.
  4. typeof window.undefined gives you "undefined" - again, just a string.
  5. typeof "undefined" gives "string", just like typeof "foo" would.
  6. Therefore, typeof typeof undefined gives "string".

In relation to this syntax:

[1, 2][1];

That's not a multi-dimensional array - it is merely creating an array first arr = [1, 2], and then selecting element 1 from it: arr[1].


undefined is a global that is undefined by default.

typeof returns a string which describes the type of the object.

So:

 [typeof x, typeof y][1];
 [typeof undefined, typeof undefined][1];
 ["undefined", "undefined"][1];
 "undefined"

 typeof "undefined" == "string"
 typeof undefined == "undefined"
 typeof 1 == "number"
 typeof {} == "object"

Also, what kind of array syntax is that?

It is an array literal with [1] on the end so it returns the object at index 1.

"Javascript The Good Parts" says that there are no multidimensional arrays.

There aren't, but an array can contain other arrays. This one doesn't though.


Whoa, this is a tough one to explain. The "typeof" operator returns a string, describing the type of its operand. So:

typeof undefined

returns the string "undefined", and

typeof typeof undefined

returns the string "string", which is the type of the string "undefined". I think it's confusing because undefined is both a type and a value.

Second part: there are indeed no multidimensional arrays (as such) in JavaScript. In this expression:

var x = [typeof x, typeof y][1];

The first set of square brackets is an array literal consisting of 2 elements. The second set of square brackets references element 1 of that array (typeof y). So that expression is effectively equivalent to this:

var x = typeof y;


  1. [typeof x, typeof y] is a normal array containing something like ["string", "number"] or possibly ["undefined", "undefined"], depending on the types of x and y.
  2. x = ["string", "number"][1] takes the second element of that array and assigns it to x.
  3. typeof x returns the type of x as the string "string".
  4. typeof "string" is "string".

As to what the difference between "undefined" and undefined is: one is a string, the other an object. typeof always returns the type of the variable as a string, because you can redefine undefined to something else so you couldn't properly compare it anymore, but "undefined" == "undefined" is always true.


"undefined" is a string (literal), undefined is ehr ... undefined

var x = [typeof x, typeof y][1] supposedly returns the string "undefined" (from typeof y). Now if you ask for typeof "undefined", it returns the string "string". And if you ask for the typeof "string" It (again) returns "string" ofcourse.

It's safe to say that typeof [anything] always returns a string (literal), so typeof typeof something would always be "string".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜