Skip to content Skip to sidebar Skip to footer

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 t

Solution 1:

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

Solution 2:

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];
 [typeofundefined, typeofundefined][1];
 ["undefined", "undefined"][1];
 "undefined"typeof"undefined" == "string"typeofundefined == "undefined"typeof1 == "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.

Solution 3:

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

typeofundefined

returns the string "undefined", and

typeoftypeofundefined

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;

Solution 4:

  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.

Solution 5:

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

Post a Comment for "What Is The Difference Between "undefined" And Undefined?"