Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
160 views
in Technique[技术] by (71.8m points)

javascript - 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 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

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

1 Reply

0 votes
by (71.8m points)
  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].


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...