blob: 867bda689fb387530b961521df581f752003f0ed (
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
|
---
title: 不等于 (!=)
slug: Web/JavaScript/Reference/Operators/Inequality
tags:
- JavaScript
- 参考
- 语言特征
- 运算符
translation_of: Web/JavaScript/Reference/Operators/Inequality
---
<div>{{jsSidebar("Operators")}}</div>
<p>不等于运算符 (<code>!=</code>) 检查其两个操作数是否不相等,并返回布尔结果。 与严格的不等式运算符不同,它尝试转换和比较不同类型的操作数。</p>
<div>{{EmbedInteractiveExample("pages/js/expressions-inequality.html")}}</div>
<p class="hidden"><font><font>该交互式示例的源代码存储在GitHub存储库中。</font><font>如果您想为交互式示例项目做出贡献,请克隆</font></font><a href="https://github.com/mdn/interactive-examples"><font><font>https://github.com/mdn/interactive-examples</font></font></a><font><font>并向我们发送请求请求。</font></font></p>
<h2 id="语法">语法</h2>
<pre class="syntaxbox notranslate">x != y</pre>
<h2 id="描述"><font><font>描述</font></font></h2>
<p><font><font>不等式运算符检查其操作数是否不相等。</font><font>这是</font></font><a href="https://wiki.developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Equality"><font><font>等于</font></font></a><font><font>运算符</font><font>的取反,</font><font>因此以下两行将始终给出相同的结果:</font></font> </p>
<pre class="brush: js notranslate">x != y
!(x == y)</pre>
<p><font><font>有关比较算法的详细信息,请参见</font></font><a href="https://wiki.developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Equality"><font><font>等于</font></font></a><font><font>运算符</font><font>的页面</font><font>。</font></font></p>
<p><font><font>与等于运算符一样,不等于运算符将尝试转换和比较不同类型的操作数:</font></font></p>
<pre class="brush: js notranslate">3 != "3"; // false</pre>
<p><font><font>为避免这种情况,并要求将不同类型视为不同,请使用</font></font><a href="https://wiki.developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_inequality"><font><font>严格的不等于</font></font></a><font><font>运算符:</font></font></p>
<pre class="brush: js notranslate">3 !== "3"; // true</pre>
<h2 id="例子"><font><font>例子</font></font></h2>
<h3 id="没有类型转换的比较"><font><font>没有类型转换的比较</font></font></h3>
<pre class="brush: js notranslate">1 != 2; // true
"hello" != "hola"; // true
1 != 1; // false
"hello" != "hello"; // false</pre>
<h3 id="与类型转换比较"><font><font>与类型转换比较</font></font></h3>
<pre class="brush: js notranslate">"1" != 1; // false
1 != "1"; // false
0 != false; // false
0 != null; // true
0 != undefined; // true
0 != !!null; // false, look at Logical NOT operator
0 != !!undefined; // false, look at Logical NOT operator
null != undefined; // false
const number1 = new Number(3);
const number2 = new Number(3);
number1 != 3; // false
number1 != number2; // true</pre>
<h3 id="对象比较"><font><font>对象比较</font></font></h3>
<pre class="brush: js notranslate">const object1 = {"key": "value"}
const object2 = {"key": "value"};
object1 != object2 // true
object2 != object2 // false</pre>
<h2 id="规范"><font><font>规范</font></font></h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Specification</th>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#sec-equality-operators', 'Equality operators')}}</td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性"><font><font>浏览器兼容性</font></font></h2>
<p>{{Compat("javascript.operators.inequality")}}</p>
<h2 id="参见">参见</h2>
<ul>
<li><a href="https://wiki.developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Equality">Equality operator</a></li>
<li><a href="https://wiki.developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_equality">Strict equality operator</a></li>
<li><a href="https://wiki.developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_inequality">Strict inequality operator</a></li>
</ul>
<div id="gtx-trans" style="position: absolute; left: 16px; top: 1743.2px;">
<div class="gtx-trans-icon"></div>
</div>
|