aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/operators/strict_equality/index.html
blob: 1b8211841a157cf18fb94b2cdd141f849d6d2dcc (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
---
title: Strict equality (===)
slug: Web/JavaScript/Reference/Operators/Strict_equality
translation_of: Web/JavaScript/Reference/Operators/Strict_equality
---
<div>{{jsSidebar("Operators")}}</div>

<p>全等运算符 (===) 会检查它的两个操作数是否相等,并且返回一个布尔值结果。与相等运算符不同,全等运算符总是认为不同类型的操作数是不同的。</p>

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

<div class="hidden">The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> and send us a pull request.</div>

<h2 id="语法">语法</h2>

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

<h2 id="描述">描述</h2>

<p>全等运算符(<code>===</code><code>!==</code>)使用<a class="external external-icon" href="http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.6" rel="noopener">全等比较算法</a>来比较两个操作数。</p>

<ul>
 <li>如果操作数的类型不同,则返回 <code>false</code></li>
 <li>如果两个操作数都是对象,只有当它们指向同一个对象时才返回 <code>true</code></li>
 <li>如果两个操作数都为 <code>null</code>,或者两个操作数都为 <code>undefined</code>,返回 <code>true</code></li>
 <li>如果两个操作数有任意一个为 <code>NaN</code>,返回 <code>false</code></li>
 <li>否则,比较两个操作数的值:
  <ul>
   <li>数字类型必须拥有相同的数值。<code>+0</code><code>-0</code> 会被认为是相同的值。</li>
   <li>字符串类型必须拥有相同顺序的相同字符。</li>
   <li>布尔运算符必须同时为 <code>true</code> 或同时为 <code>false</code></li>
  </ul>
 </li>
</ul>

<p>全等运算符与<a href="/zh-CN/docs/Web/JavaScript/Reference/Operators/Equality">相等运算符</a><code>==</code>)最显著的区别是,如果操作数的类型不同,<code>== </code>运算符会在比较之前尝试将它们转换为相同的类型。</p>

<h2 id="例子">例子</h2>

<h3 id="比较相同类型的操作数">比较相同类型的操作数</h3>

<pre class="brush: js">console.log("hello" === "hello");   // true
console.log("hello" === "hola");    // false

console.log(3 === 3);               // true
console.log(3 === 4);               // false

console.log(true === true);         // true
console.log(true === false);        // false

console.log(null === null);         // true</pre>

<h3 id="比较不同类型的操作数">比较不同类型的操作数</h3>

<pre class="brush: js">console.log("3" === 3);           // false

console.log(true === 1);          // false

console.log(null === undefined);  // false</pre>

<h3 id="比较对象">比较对象</h3>

<pre class="brush: js">const object1 = {
  name: "hello"
}

const object2 = {
  name: "hello"
}

console.log(object1 === object2);  // false
console.log(object1 === object1);  // true</pre>

<h2 id="Specifications">Specifications</h2>

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

<h2 id="浏览器兼容性">浏览器兼容性</h2>



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

<h2 id="See_also">See also</h2>

<ul>
 <li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Equality">Equality operator</a></li>
 <li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Inequality">Inequality operator</a></li>
 <li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Strict_inequality">Strict inequality operator</a></li>
</ul>