JavaScript_Succinctly
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Chapter 8 Undefined<br />
Conceptual overview of the undefined value<br />
The undefined value is used by <strong>JavaScript</strong> in two slightly different ways.<br />
The first way it's used is to indicate that a declared variable (e.g., var foo) has no<br />
assigned value. The second way it's used is to indicate that an object property you’re<br />
trying to access is not defined (i.e. it has not even been named), and is not found in the<br />
prototype chain.<br />
In the following sample, I examine both usages of undefined by <strong>JavaScript</strong>.<br />
Sample: sample62.html<br />
<br />
var initializedVariable; // Declare variable.<br />
console.log(initializedVariable); // Logs undefined.<br />
console.log(typeof initializedVariable); // Confirm that <strong>JavaScript</strong><br />
returns undefined.<br />
var foo = {};<br />
console.log(foo.bar); // Logs undefined, no bar property in foo object.<br />
console.log(typeof foo.bar); // Confirm that <strong>JavaScript</strong> returns<br />
undefined.<br />
<br />
Notes<br />
It is considered good practice to allow <strong>JavaScript</strong> alone to use undefined. You should<br />
never find yourself setting a value to undefined, as in foo = undefined. Instead, null<br />
should be used if you are specifying that a property or variable value is not available.<br />
<strong>JavaScript</strong> ECMA-262 Edition 3 (and later) declares the undefined<br />
variable in the global scope<br />
Unlike previous versions, <strong>JavaScript</strong> ECMA-262 Edition 3 (and later) has a global<br />
variable called undefined declared in the global scope. Because the variable is<br />
declared and not assigned a value, the undefined variable is set to undefined.<br />
76