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.
undefined
is actually window.undefined
in most situations. It's just a variable.window.undefined
happens to not be defined, unless someone defines it (try undefined = 1
and typeof undefined
will be "number"
).typeof
is an operator that always returns a string, describing the type of a value.typeof window.undefined
gives you "undefined"
- again, just a string.typeof "undefined"
gives "string"
, just like typeof "foo"
would.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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With