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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
---
title: 比較運算子
slug: Web/JavaScript/Reference/Operators/Comparison_Operators
translation_of: Web/JavaScript/Reference/Operators
translation_of_original: Web/JavaScript/Reference/Operators/Comparison_Operators
---
<div>{{jsSidebar("Operators")}}</div>
<p>JavaScript has both strict and type–converting comparisons. A strict comparison (e.g., <code>===</code>) is only true if the operands are of the same type and the contents match. The more commonly-used abstract comparison (e.g. <code>==</code>) converts the operands to the same type before making the comparison. For relational abstract comparisons (e.g., <code><=</code>), the operands are first converted to primitives, then to the same type, before comparison.</p>
<p>Strings are compared based on standard lexicographical ordering, using Unicode values.</p>
<p>Features of comparisons:</p>
<ul>
<li>Two strings are strictly equal when they have the same sequence of characters, same length, and same characters in corresponding positions.</li>
<li>Two numbers are strictly equal when they are numerically equal (have the same number value). <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/NaN" title="NaN">NaN</a> is not equal to anything, including NaN. Positive and negative zeros are equal to one another.</li>
<li>Two Boolean operands are strictly equal if both are <code>true</code> or both are <code>false</code>.</li>
<li>Two distinct objects are never equal for either strict or abstract comparisons.</li>
<li>An expression comparing Objects is only true if the operands reference the same Object.</li>
<li>Null and Undefined Types are strictly equal to themselves and abstractly equal to each other.</li>
</ul>
<h2 id="Equality_operators">Equality operators</h2>
<h3 id="Equality_()"><a name="Equality">Equality (==)</a></h3>
<p>The equality operator converts the operands if they are <strong>not of the same type</strong>, then applies strict comparison. If <strong>both operands are objects</strong>, then JavaScript compares internal references which are equal when operands refer to the same object in memory.</p>
<h4 id="Syntax">Syntax</h4>
<pre class="syntaxbox">x == y
</pre>
<h4 id="Examples">Examples</h4>
<pre class="brush: js"> 1 == 1 // true
"1" == 1 // true
1 == '1' // true
0 == false // true
0 == null // false
var object1 = {"value":"key"}, object2={"value":"key"};
object1 == object2 //false
0 == undefined // false
null == undefined // true
</pre>
<h3 id="Inequality_(!)"><a name="Inequality">Inequality (!=)</a></h3>
<p>The inequality operator returns true if the operands are not equal. If the two operands are <strong>not of the same type</strong>, JavaScript attempts to convert the operands to an appropriate type for the comparison. If <strong>both operands are objects</strong>, then JavaScript compares internal references which are not equal when operands refer to different objects in memory.</p>
<h4 id="Syntax_2">Syntax</h4>
<pre class="syntaxbox">x != y</pre>
<h4 id="Examples_2">Examples</h4>
<pre class="brush: js">1 != 2 // true
1 != "1" // false
1 != '1' // false
1 != true // false
0 != false // false
</pre>
<h3 id="Identity_strict_equality_()"><a name="Identity">Identity / strict equality (===)</a></h3>
<p>The identity operator returns true if the operands are strictly equal (see above) <strong>with no type conversion</strong>. </p>
<h4 id="Syntax_3">Syntax</h4>
<pre class="syntaxbox">x === y</pre>
<h4 id="Examples_3">Examples</h4>
<pre class="brush: js ">3 === 3 // true
3 === '3' // false
var object1 = {"value":"key"}, object2={"value":"key"};
object1 === object2 //false</pre>
<p> </p>
<h3 id="Non-identity_strict_inequality_(!)"><a name="Nonidentity">Non-identity / strict inequality (!==)</a></h3>
<p>The non-identity operator returns true if the operands <strong>are not equal and/or not of the same type</strong>.</p>
<h4 id="Syntax_4">Syntax</h4>
<pre class="syntaxbox">x !== y</pre>
<h4 id="Examples_4">Examples</h4>
<pre class="brush: js">3 !== '3' // true
4 !== 3 // true
</pre>
<h2 id="Relational_operators">Relational operators</h2>
<p>Each of these operators will call the <code>valueOf()</code> function on each operand before a comparison is made.</p>
<h3 id="Greater_than_operator_(>)"><a name="Greater_than_operator">Greater than operator (>)</a></h3>
<p>The greater than operator returns true if the left operand is greater than the right operand.</p>
<h4 id="Syntax_5">Syntax</h4>
<pre class="syntaxbox">x > y</pre>
<h4 id="Examples_5">Examples</h4>
<pre class="brush: js">4 > 3 // true
</pre>
<h3 id="Greater_than_or_equal_operator_(>)"><a name="Greater_than_or_equal_operator">Greater than or equal operator (>=)</a></h3>
<p>The greater than or equal operator returns true if the left operand is greater than or equal to the right operand.</p>
<h4 id="Syntax_6">Syntax</h4>
<pre class="syntaxbox"> x >= y</pre>
<h4 id="Examples_6">Examples</h4>
<pre class="brush: js">4 >= 3 // true
3 >= 3 // true
</pre>
<h3 id="Less_than_operator_(<)"><a name="Less_than_operator">Less than operator (<)</a></h3>
<p>The less than operator returns true if the left operand is less than the right operand.</p>
<h4 id="Syntax_7">Syntax</h4>
<pre class="syntaxbox"> x < y</pre>
<h4 id="Examples_7">Examples</h4>
<pre class="brush: js">3 < 4 // true
</pre>
<h3 id="Less_than_or_equal_operator_(<)"><a id="Less_than_or_equal_operator" name="Less_than_or_equal_operator">Less than or equal operator (<=)</a></h3>
<p>The less than or equal operator returns true if the left operand is less than or equal to the right operand.</p>
<h4 id="Syntax_8">Syntax</h4>
<pre class="syntaxbox"> x <= y</pre>
<h4 id="Examples_8">Examples</h4>
<pre class="brush: js">3 <= 4 // true
</pre>
<h2 id="Using_the_Equality_Operators">Using the Equality Operators</h2>
<p>The standard equality operators (<code>==</code> and <code>!=</code>) use the <a href="http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3">Abstract Equality Comparison Algorithm</a> to compare two operands. If the operands are of different types, it will attempt to convert them to the same type before making the comparison, e.g., in the expression <code>5 == '5'</code>, the string on the right is converted to {{jsxref("Number")}} before the comparison is made.</p>
<p>The strict equality operators (<code>===</code> and <code>!==</code>) use the <a href="http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.6">Strict Equality Comparison Algorithm</a> and are intended for performing equality comparisons on operands of the same type. If the operands are of different types, the result is always <code>false</code> so <code>5 !== '5'</code>.</p>
<p>Use strict equality operators if the operands must be of a specific type as well as value or if the exact type of the operands is important. Otherwise, use the standard equality operators, which allow you to compare the identity of two operands even if they are not of the same type.</p>
<p>When type conversion is involved in the comparison (i.e., non–strict comparison), JavaScript converts the types {{jsxref("String")}}, {{jsxref("Number")}}, {{jsxref("Boolean")}}, or {{jsxref("Object")}} operands as follows:</p>
<ul>
<li>When comparing a number and a string, the string is converted to a number value. JavaScript attempts to convert the string numeric literal to a <code>Number</code> type value. First, a mathematical value is derived from the string numeric literal. Next, this value is rounded to nearest <code>Number</code> type value.</li>
<li>If one of the operands is <code>Boolean</code>, the Boolean operand is converted to 1 if it is <code>true</code> and +0 if it is <code>false</code>.</li>
<li>If an object is compared with a number or string, JavaScript attempts to return the default value for the object. Operators attempt to convert the object to a primitive value, a <code>String</code> or <code>Number</code> value, using the <code>valueOf</code> and <code>toString</code> methods of the objects. If this attempt to convert the object fails, a runtime error is generated.</li>
<li>Note that an object is converted into a primitive if, and only if, its comparand is a primitive. If both operands are objects, they're compared as objects, and the equality test is true only if both refer the same object.</li>
</ul>
<div class="note"><strong>Note:</strong> String objects are Type Object, not String! String objects are rarely used, so the following results might be surprising:</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">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
<tr>
<td>{{SpecName('ES1')}}</td>
<td>{{Spec2('ES1')}}</td>
<td>Initial definition. Implemented in JavaScript 1.0</td>
</tr>
<tr>
<td>{{SpecName('ES3')}}</td>
<td>{{Spec2('ES3')}}</td>
<td>Adds <code>===</code> and <code>!==</code> operators. Implemented in JavaScript 1.3</td>
</tr>
<tr>
<td>{{SpecName('ES5.1', '#sec-11.8')}}</td>
<td>{{Spec2('ES5.1')}}</td>
<td>Defined in several sections of the specification: <a href="http://www.ecma-international.org/ecma-262/5.1/#sec-11.8">Relational Operators</a>, <a href="http://www.ecma-international.org/ecma-262/5.1/#sec-11.9">Equality Operators</a></td>
</tr>
<tr>
<td>{{SpecName('ES6', '#sec-relational-operators')}}</td>
<td>{{Spec2('ES6')}}</td>
<td>Defined in several sections of the specification: <a href="http://www.ecma-international.org/ecma-262/6.0/#sec-relational-operators">Relational Operators</a>, <a href="http://www.ecma-international.org/ecma-262/6.0/#sec-equality-operators">Equality Operators</a></td>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#sec-relational-operators')}}</td>
<td>{{Spec2('ESDraft')}}</td>
<td>Defined in several sections of the specification: <a href="http://tc39.github.io/ecma262/#sec-relational-operators">Relational Operators</a>, <a href="http://tc39.github.io/ecma262/#sec-equality-operators">Equality Operators</a></td>
</tr>
</tbody>
</table>
<h2 id="瀏覽器相容性">瀏覽器相容性</h2>
<p>{{CompatibilityTable}}</p>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Chrome</th>
<th>Firefox (Gecko)</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari</th>
</tr>
<tr>
<td>Basic support</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
</tr>
</tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Android</th>
<th>Chrome for Android</th>
<th>Firefox Mobile (Gecko)</th>
<th>IE Mobile</th>
<th>Opera Mobile</th>
<th>Safari Mobile</th>
</tr>
<tr>
<td>Basic support</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
</tr>
</tbody>
</table>
</div>
<h2 id="參見">參見</h2>
<ul>
<li>{{jsxref("Object.is()")}}</li>
<li>{{jsxref("Math.sign()")}}</li>
<li><a href="/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness">Equality comparisons and sameness</a></li>
</ul>
|