Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between "undefined" and undefined?

Tags:

javascript

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.

like image 609
Blub Avatar asked Apr 14 '11 12:04

Blub


3 Answers

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

like image 79
David Tang Avatar answered Oct 24 '22 05:10

David Tang


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.

like image 38
Quentin Avatar answered Oct 24 '22 07:10

Quentin


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;
like image 1
Anodyne Avatar answered Oct 24 '22 07:10

Anodyne