aboutsummaryrefslogtreecommitdiff
path: root/files/ko/conflicting/web/javascript/reference/operators_310dc67549939233c3d18a8fa2cdbb23/index.html
blob: cf5ae3afa259e0a9dda1e5cf3472b1c0156c677c (plain)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
---
title: 비교 연산자
slug: Web/JavaScript/Reference/Operators/Comparison_Operators
tags:
  - JavaScript
  - Operator
  - Reference
translation_of: Web/JavaScript/Reference/Operators
translation_of_original: Web/JavaScript/Reference/Operators/Comparison_Operators
---
<div>{{jsSidebar("Operators")}}</div>

<p>JavaScript는 엄격한 비교와 형변환 비교 두 가지의 비교 방법을 모두 가지고 있습니다. 엄격(일치) 비교(<code>===</code>)는 두 피연산자가 같은 자료형에, 그 내용도 일치해야만 참입니다. 추상(동등) 비교(<code>==</code>)는 비교 전에 두 피연산자를 동일한 자료형으로 변환합니다. 관계 추상 비교(<code>&lt;=</code>)의 경우 {{glossary("primitive", "원시값")}}으로 바꾸고, 같은 자료형으로 다시 바꾼후 비교를 수행합니다.</p>

<p>문자열의 경우 {{glossary("unicode", "유니코드")}} 값을 사용한 사전순으로 비교합니다.</p>

<div>{{EmbedInteractiveExample("pages/js/expressions-comparisonoperators.html")}}</div>



<p>비교 연산의 특징은 다음과 같습니다.</p>

<ul>
 <li>두 문자열이 같은 문자 시퀀스로 구성되고, 같은 길이를 가지며, 같은 위치에 같은 문자가 존재하면 일치합니다.</li>
 <li>두 숫자는 숫자로서 같은 값(같은 숫자 값)이면 일치합니다. {{jsxref("NaN")}}은 자기 자신을 포함한 그 무엇과도 동등하지 않습니다. <code>+0</code><code>-0</code>은 서로 일치합니다.</li>
 <li>두 불리언은 둘 다 <code>true</code>거나 <code>false</code>이면 일치합니다.</li>
 <li>서로 다른 두 객체는 절대 일치하지도, 동등하지도 않습니다.</li>
 <li>객체를 비교하는 표현식은 두 피연산자가 동일한 객체를 참조하는 경우에만 참입니다.</li>
 <li>{{jsxref("null")}}{{jsxref("undefined")}}는 자기 자신과 일치하며, 서로 동등합니다.</li>
</ul>

<h2 id="동치_연산자">동치 연산자</h2>

<h3 id="동등_연산자"><a name="Equality">동등 연산자 (==)</a></h3>

<p>동등 연산자는 두 피연산자의 자료형이 같지 않은 경우 같아지도록 변환한 후, 엄격 비교를 수행합니다. 피연산자가 모두 객체라면, JavaScript는 내부 참조를 보고, 둘 다 메모리의 같은 객체를 바라보고 있는지 판별합니다.</p>

<h4 id="구문">구문</h4>

<pre class="syntaxbox">x == y
</pre>

<h4 id="예제">예제</h4>

<pre class="brush: js">  1   ==  1        // true
 "1"  ==  1        // true
  1   == '1'       // true
  0   == false     // true
  0   == null      // false

  0   == undefined // false
null  == undefined // true
</pre>

<h3 id="부등_연산자_!"><a name="Inequality">부등 연산자 (!=)</a></h3>

<p>부등 연산자는 두 피연산자가 같지 않은 경우 참을 반환합니다. 피연산자의 자료형이 일치하지 않는 경우 적절한 자료형으로의 변환을 시도합니다. 피연산자가 모두 객체라면, JavaScript는 내부 참조를 보고, 서로 메모리의 다른 객체를 바라보고 있는지 판별합니다.</p>

<h4 id="구문_2">구문</h4>

<pre class="syntaxbox">x != y</pre>

<h4 id="예제_2">예제</h4>

<pre class="brush: js">1 !=   2     // true
1 !=  "1"    // false
1 !=  '1'    // false
1 !=  true   // false
0 !=  false  // false
</pre>

<h3 id="일치_연산자"><a name="Identity">일치 연산자 (===)</a></h3>

<p>일치 연산자는 자료형 변환 없이 두 연산자가 엄격히 같은지 판별합니다.</p>

<h4 id="구문_3">구문</h4>

<pre class="syntaxbox">x === y</pre>

<h4 id="예제_3">예제</h4>

<pre class="brush: js ">3 === 3   // true
3 === '3' // false</pre>

<h3 id="불일치_연산자_!"><a name="Nonidentity">불일치 연산자 (!==)</a></h3>

<p>일치 연산자는 두 연산자가 같지 않거나, 같은 자료형이 아닐 때 참을 반환합니다.</p>

<h4 id="구문_4">구문</h4>

<pre class="syntaxbox">x !== y</pre>

<h4 id="예제_4">예제</h4>

<pre class="brush: js">3 !== '3' // true
4 !== 3   // true
</pre>

<h2 id="관계_연산자">관계 연산자</h2>

<p>이 항목의 모든 연산자는 비교 전에 피연산자를 {{glossary("primitive", "원시값")}}으로 <a href="/ko/docs/Glossary/Type_coercion">변환</a>합니다. 둘 다 문자열이 되는 경우 사전순으로 비교하고, 그렇지 않으면 숫자로 변환합니다. {{jsxref("NaN")}}과의 비교는 항상 <code>false</code>를 반환합니다.</p>

<h3 id="초과_연산자_>"><a name="Greater_than_operator">초과 연산자 (&gt;)</a></h3>

<p>초과 연산자는 왼쪽 피연산자가 오른쪽 피연산자보다 큰 경우 참을 반환합니다.</p>

<h4 id="구문_5">구문</h4>

<pre class="syntaxbox">x &gt; y</pre>

<h4 id="예제_5">예제</h4>

<pre class="brush: js">4 &gt; 3 // true
</pre>

<h3 id="이상_연산자_>"><a name="Greater_than_or_equal_operator">이상 연산자 (&gt;=)</a></h3>

<p>이상 연산자는 왼쪽 피연산자가 오른쪽 피연산자보다 크거나 같으면 참을 반환합니다.</p>

<h4 id="구문_6">구문</h4>

<pre class="syntaxbox"> x &gt;= y</pre>

<h4 id="예제_6">예제</h4>

<pre class="brush: js">4 &gt;= 3 // true
3 &gt;= 3 // true
</pre>

<h3 id="미만_연산자_&lt;"><a name="Less_than_operator">미만 연산자 (&lt;)</a></h3>

<p>미만 연산자는 왼쪽 피연산자가 오른쪽 피연산자보다 작은 경우 참을 반환합니다.</p>

<h4 id="구문_7">구문</h4>

<pre class="syntaxbox">x &lt; y</pre>

<h4 id="예제_7">예제</h4>

<pre class="brush: js">3 &lt; 4 // true</pre>

<h3 id="이하_연산자_&lt;"><a id="Less_than_or_equal_operator" name="Less_than_or_equal_operator">이하 연산자 (&lt;=)</a></h3>

<p>이하 연산자는 왼쪽 피연산자가 오른쪽 피연산자보다 작거나 같으면 참을 반환합니다.</p>

<h4 id="구문_8">구문</h4>

<pre class="syntaxbox"> x &lt;= y</pre>

<h4 id="예제_8">예제</h4>

<pre class="brush: js">3 &lt;= 4 // true
</pre>

<h2 id="동치_연산자_사용하기">동치 연산자 사용하기</h2>

<p>표준 동치, 동등 연산자(<code>==</code>, <code>!=</code>)는 두 피연산자를 비교하기 위해 <a href="http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3">추상 동치 비교 알고리즘</a>(Abstract Equlity Comparison Algorithm)을 사용합니다. 피연산자 간 자료형이 일치하지 않으면 우선 변환을 시도합니다. 예를 들어 표현식 <code>5 == '5'</code>에서는 비교 전 오른쪽 문자열을 {{jsxref("Number")}}로 변환합니다.</p>

<p>엄격 동치, 일치 연산자(<code>===</code>, <code>!==</code>)는 <a href="http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.6">엄격 동치 비교 알고리즘</a>(Strict Equality Comparison Algorithm)을 사용하며, 같은 자료형을 가진 피연산자를 비교하기 위해 사용합니다. 피연산자 간 자료형이 일치하지 않으면 항상 <code>false</code>이므로, <code>5 !== '5'</code>입니다.</p>

<p>피연산자의 값은 물론 특정 자료형이어야 하는 경우 일치 연산자를 사용하세요. 그렇지 않은 경우 형변환을 자동으로 해주는 동등 연산자를 사용할 수도 있습니다.</p>

<p>비교 과정에 자료형 변환이 필요한 경우 JavaScript는 {{jsxref("String")}}, {{jsxref("Number")}}, {{jsxref("Boolean")}}, {{jsxref("Object")}} 자료형을 다음과 같이 변환합니다.</p>

<ul>
 <li>숫자와 문자열을 비교할 땐 문자열을 숫자로 변환합니다. 우선, 문자열에서 수학적 값을 도출합니다. 그 후 가장 가까운 <code>Number</code> 자료형 값으로 반올림합니다.</li>
 <li>하나의 연산자가 <code>Boolean</code>인 경우, <code>true</code><code>1</code>, <code>false</code><code>0</code>으로 변환합니다.</li>
 <li>객체를 문자열이나 숫자와 비교하는 경우, JavaScript는 객체의 기본값을 사용합니다. 연산자는 우선 객체의 <code>valueOf()</code> 또는 <code>toString()</code> 메서드를 사용해 문자열 혹은 숫자 값을 받으려 시도합니다. 실패할 경우 런타임 오류가 발생합니다.</li>
 <li>객체를 원시값과 비교하는 경우에만 객체의 변환이 발생합니다. 두 연산자가 모두 객체인 경우 변환하지 않으며, 둘 다 같은 객체를 참조하는 경우 참을 반환합니다.</li>
</ul>

<div class="note"><strong>참고:</strong> <code>String</code> 객체는 자료형 객체지, 문자열이 아닙니다! <code>String</code> 객체는 거의 쓰이지 않으며, 이런 성질로 인해 아래의 결과는 예상치 못한 값일 수 있습니다.</div>

<pre class="brush:js">// true as both operands are type String (i.e. string primitives):
'foo' === 'foo'

var a = new String('foo');
var b = new String('foo');

// false as a and b are type Object and reference different objects
a == b

// false as a and b are type Object and reference different objects
a === b

// true as a and 'foo' are of different type and, the Object (a)
// is converted to String 'foo' before comparison
a == 'foo'</pre>

<h2 id="명세">명세</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Status</th>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-equality-operators', 'Equality Operators')}}</td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-relational-operators', 'Relational Operators')}}</td>
  </tr>
 </tbody>
</table>

<h2 id="브라우저_호환성">브라우저 호환성</h2>

<p>{{Compat("javascript.operators.comparison")}}</p>

<h2 id="같이_보기">같이 보기</h2>

<ul>
 <li>{{jsxref("Object.is()")}}</li>
 <li>{{jsxref("Math.sign()")}}</li>
 <li><a href="/ko/docs/Web/JavaScript/Equality_comparisons_and_sameness">동치 비교와 동일성</a></li>
</ul>