What Is The Difference Between "undefined" And Undefined?
Solution 1:
undefinedis actuallywindow.undefinedin most situations. It's just a variable.window.undefinedhappens to not be defined, unless someone defines it (tryundefined = 1andtypeof undefinedwill be"number").typeofis an operator that always returns a string, describing the type of a value.typeof window.undefinedgives you"undefined"- again, just a string.typeof "undefined"gives"string", just liketypeof "foo"would.- Therefore,
typeof typeof undefinedgives"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:
typeofundefinedreturns the string "undefined", and
typeoftypeofundefinedreturns 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:
[typeof x, typeof y]is a normal array containing something like["string", "number"]or possibly["undefined", "undefined"], depending on the types ofxandy.x = ["string", "number"][1]takes the second element of that array and assigns it tox.typeof xreturns the type ofxas the string"string".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?"