1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
---
title: undefined
slug: Web/JavaScript/Reference/Global_Objects/undefined
tags:
- JavaScript
translation_of: Web/JavaScript/Reference/Global_Objects/undefined
---
<div>{{jsSidebar("Objects")}}</div>
<p>The global <code><strong>undefined</strong></code> property represents the primitive value <code>{{Glossary("Undefined", "undefined")}}</code>. It is one of JavaScript's {{Glossary("Primitive", "primitive types")}}.</p>
<p>{{js_property_attributes(0,0,0)}}</p>
<div>{{EmbedInteractiveExample("pages/js/globalprops-undefined.html")}}</div>
<h2 id="語法">語法</h2>
<pre class="syntaxbox"><code>undefined</code></pre>
<h2 id="描述">描述</h2>
<p><code>undefined</code> is a property of the <em>global object</em>; i.e., it is a variable in global scope. The initial value of <code>undefined</code> is the primitive value <code>{{Glossary("Undefined", "undefined")}}</code>.</p>
<p>In modern browsers (JavaScript 1.8.5 / Firefox 4+), <code>undefined</code> is a non-configurable, non-writable property per the ECMAScript 5 specification. Even when this is not the case, avoid overriding it.</p>
<p>A variable that has not been assigned a value is of type undefined. A method or statement also returns <code>undefined</code> if the variable that is being evaluated does not have an assigned value. A function returns <code>undefined</code> if a value was not {{jsxref("Statements/return", "returned")}}.</p>
<div class="warning">
<p>While it is possible to use it as an {{Glossary("Identifier", "identifier")}} (variable name) in any scope other than the global scope (because <code>undefined</code> is not a {{jsxref("Reserved_Words", "reserved word")}}), doing so is a very bad idea that will make your code difficult to maintain and debug.</p>
<pre class="brush: js">//DON'T DO THIS
// logs "foo string"
(function() { var undefined = 'foo'; console.log(undefined, typeof undefined); })();
// logs "foo string"
(function(undefined) { console.log(undefined, typeof undefined); })('foo');
</pre>
</div>
<h2 id="範例">範例</h2>
<h3 id="Strict_equality_and_undefined">Strict equality and <code>undefined</code></h3>
<p>You can use <code>undefined</code> and the strict equality and inequality operators to determine whether a variable has a value. In the following code, the variable <code>x</code> is not defined, and the <code>if</code> statement evaluates to true.</p>
<pre class="brush: js">var x;
if (x === undefined) {
// these statements execute
}
else {
// these statements do not execute
}
</pre>
<div class="note">
<p>備註:The strict equality operator rather than the standard equality operator must be used here, because <code>x == undefined</code> also checks whether <code>x</code> is <code>null</code>, while strict equality doesn't. <code>null</code> is not equivalent to <code>undefined</code>. See {{jsxref("Operators/Comparison_Operators", "comparison operators")}} for details.</p>
</div>
<h3 id="Typeof_operator_and_undefined"><code>Typeof</code> operator and <code>undefined</code></h3>
<p>Alternatively, {{jsxref("Operators/typeof", "typeof")}} can be used:</p>
<pre class="brush: js">var x;
if (typeof x === 'undefined') {
// these statements execute
}
</pre>
<p>One reason to use {{jsxref("Operators/typeof", "typeof")}} is that it does not throw an error if the variable has not been declared.</p>
<pre class="brush: js">// x has not been declared before
if (typeof x === 'undefined') { // evaluates to true without errors
// these statements execute
}
if (x === undefined) { // throws a ReferenceError
}
</pre>
<p>However, this kind of technique should be avoided. JavaScript is a statically scoped language, so knowing if a variable is declared can be read by seeing whether it is declared in an enclosing context. The only exception is the global scope, but the global scope is bound to the global object, so checking the existence of a variable in the global context can be done by checking the existence of a property on the <em>global object</em> (using the {{jsxref("Operators/in", "in")}} operator, for instance).</p>
<h3 id="Void_operator_and_undefined"><code>Void</code> operator and <code>undefined</code></h3>
<p>The {{jsxref("Operators/void", "void")}} operator is a third alternative.</p>
<pre class="brush: js">var x;
if (x === void 0) {
// these statements execute
}
// y has not been declared before
if (y === void 0) {
// throws a - Uncaught ReferenceError: y is not defined
}
</pre>
<h2 id="規範">規範</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
<tr>
<td>{{SpecName('ES1', '#sec-4.3.9', 'undefined')}}</td>
<td>{{Spec2('ES1')}}</td>
<td>Initial definition. Implemented in JavaScript 1.3.</td>
</tr>
<tr>
<td>{{SpecName('ES5.1', '#sec-15.1.1.3', 'undefined')}}</td>
<td>{{Spec2('ES5.1')}}</td>
<td> </td>
</tr>
<tr>
<td>{{SpecName('ES6', '#sec-undefined', 'undefined')}}</td>
<td>{{Spec2('ES6')}}</td>
<td> </td>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#sec-undefined', 'undefined')}}</td>
<td>{{Spec2('ESDraft')}}</td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="瀏覽器相容性">瀏覽器相容性</h2>
<p>{{Compat("javascript.builtins.undefined")}}</p>
|