Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 purpose 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
143 views
Welcome To Ask or Share your Answers For Others

1 Answer

  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].


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...