Check out this fascinating wtfjs
code snippet:
var a = [,];
alert(a.indexOf(a[0]));
This example highlights the difference between uninitialized
and undefined
values:
The array a
contains only one uninitialized element.
Accessing a[0]
returns undefined
.
Since a
does not explicitly include the undefined
value, a.indexOf(a[0]) === -1
evaluates to true
.
But have you ever wondered why a[0]
returns undefined
? What internal mechanism is at play here?
P.S. Remember that undefined
is a primitive type in JavaScript. The term uninitialized
refers to a value without any specified JavaScript type, which, technically speaking, does not exist as a specific primitive type in JavaScript.