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
- Reference
translation_of: Web/JavaScript/Reference/Global_Objects/undefined
---
<div>{{jsSidebar("Objects")}}</div>
<p>전역 <code><strong>undefined</strong></code> 속성은 <code>{{Glossary("Undefined", "undefined")}}</code> 원시 값을 나타내며, JavaScript의 {{Glossary("Primitive", "원시 자료형")}} 중 하나입니다.</p>
<p>{{js_property_attributes(0,0,0)}}</p>
<div>{{EmbedInteractiveExample("pages/js/globalprops-undefined.html")}}</div>
<h2 id="구문">구문</h2>
<pre class="syntaxbox notranslate">undefined</pre>
<h2 id="설명">설명</h2>
<p><code>undefined</code>는 전역 객체의 속성입니다. 즉, 전역 스코프에서의 변수입니다. <code>undefined</code>의 초기 값은 <code>{{Glossary("Undefined", "undefined")}}</code> 원시 값입니다.</p>
<p>최신 브라우저에서 <code>undefined</code>는 ECMAScript 5 명세에 따라 설정 불가, 쓰기 불가한 속성입니다. 그렇지 않더라도 덮어쓰는건 피하는게 좋습니다.</p>
<p>값을 할당하지 않은 변수는 <code>undefined</code> 자료형입니다. 또한 메서드와 선언도 평가할 변수가 값을 할당받지 않은 경우에 <code>undefined</code>를 반환합니다. 함수는 값을 명시적으로 {{jsxref("Statements/return", "반환", "", 1)}}하지 않으면 <code>undefined</code>를 반환합니다.</p>
<div class="note">
<p><code>undefined</code>는 <a href="/ko/docs/Web/JavaScript/Reference/Reserved_Words">예약어</a>가 아니기 때문에 전역 범위 외에서 {{Glossary("Identifier", "식별자")}}(변수 이름)로 사용할 수 있습니다. 그러나 유지보수와 디버깅 시 어려움을 낳을 수 있으므로 반드시 피해야 합니다.</p>
<pre class="brush: js example-bad notranslate">// 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="일치_연산과_undefined">일치 연산과 <code>undefined</code></h3>
<p><code>undefined</code>와 일치, 불일치 연산자를 사용해 변수에 값이 할당됐는지 판별할 수 있습니다. 다음 예제에서 변수 <code>x</code>는 초기화되지 않았으므로 <code>if</code>문은 <code>true</code>로 평가됩니다.</p>
<pre class="brush: js notranslate">var x;
if (x === undefined) {
// 이 문이 실행됨
}
else {
// 이 문이 실행되지 않음
}
</pre>
<div class="note">
<p><strong>참고</strong>: 동등 연산자가 아니라 일치 연산자를 사용해야 합니다. 동등 연산자일 때 <code>x == undefined</code>는 <code>x</code>가 {{jsxref("null")}}일 때도 참이기 때문입니다. 즉 <code>null</code>은 <code>undefined</code>와 동등하지만, 일치하지는 않습니다.</p>
<p>자세한 내용은 {{jsxref("Operators/Comparison_Operators", "비교 연산자", "", 1)}} 문서를 확인하세요.</p>
</div>
<h3 id="typeof_연산자와_undefined"><code>typeof</code> 연산자와 <code>undefined</code></h3>
<p>위의 예제 대신 {{jsxref("Operators/typeof", "typeof")}}를 사용할 수도 있습니다.</p>
<pre class="brush: js notranslate">var x;
if (typeof x === 'undefined') {
// 이 문이 실행됨
}
</pre>
<p><code>typeof</code>를 사용하는 이유 중 하나는 선언하지 않은 변수를 사용해도 오류를 던지지 않기 때문입니다.</p>
<pre class="brush: js notranslate">// x를 선언한 적 없음
if (typeof x === 'undefined') { // 오류 없이 true로 평가
// 이 문이 실행됨
}
if(x === undefined) { // ReferenceError
}
</pre>
<p>그러나 다른 방법도 있습니다. JavaScript는 정적 범위를 가지는 언어이므로, 변수의 선언 여부는 현재 맥락의 코드를 읽어 알 수 있습니다.</p>
<p>전역 범위는 {{jsxref("globalThis", "전역 객체", "", 1)}}에 묶여 있으므로, 전역 맥락에서 변수의 존재 유무는 {{jsxref("Operators/in", "in")}} 연산자를 전역 객체 대상으로 실행해 알 수 있습니다. 즉,</p>
<pre class="brush: js notranslate">if ('x' in window) {
// x가 전역으로 정의된 경우 이 문이 실행됨
}</pre>
<h3 id="void_연산자와_undefined"><code>void</code> 연산자와 <code>undefined</code></h3>
<p>{{jsxref("Operators/void", "void")}} 연산자를 제 3의 대안으로 사용할 수 있습니다.</p>
<pre class="brush: js notranslate">var x;
if (x === void 0) {
// 이 문이 실행됨
}
// y를 선언한 적 없음
if (y === void 0) {
// Uncaught Reference Error: y is not defined
}
</pre>
<h2 id="명세">명세</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">명세</th>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#sec-undefined', 'undefined')}}</td>
</tr>
</tbody>
</table>
<h2 id="브라우저_호환성">브라우저 호환성</h2>
<p>{{Compat("javascript.builtins.undefined")}}</p>
<h2 id="같이_보기">같이 보기</h2>
<ul>
<li>JavaScript의 {{glossary("Primitive", "원시 값")}}</li>
<li>{{jsxref("null")}}</li>
</ul>
|