aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/operators/inequality/index.html
blob: 11b16d1c2e8da0e878777594e071e486fa03f2cb (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">该交互式示例的源代码存储在GitHub存储库中。如果您想为交互式示例项目做出贡献,请克隆<a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a>并向我们​​发送请求请求。</p>

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

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

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

<p>不等式运算符检查其操作数是否不相等。这是<a href="/zh-CN/docs/Web/JavaScript/Reference/Operators/Equality">等于</a>运算符的取反,因此以下两行将始终给出相同的结果: </p>

<pre class="brush: js notranslate">x != y

!(x == y)</pre>

<p>有关比较算法的详细信息,请参见<a href="/zh-CN/docs/Web/JavaScript/Reference/Operators/Equality">等于</a>运算符的页面。</p>

<p>与等于运算符一样,不等于运算符将尝试转换和比较不同类型的操作数:</p>

<pre class="brush: js notranslate">3 != "3"; // false</pre>

<p>为避免这种情况,并要求将不同类型视为不同,请使用<a href="/zh-CN/docs/Web/JavaScript/Reference/Operators/Strict_inequality">严格的不等于</a>运算符:</p>

<pre class="brush: js notranslate">3 !== "3"; // true</pre>

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

<h3 id="没有类型转换的比较">没有类型转换的比较</h3>

<pre class="brush: js notranslate">1 != 2;              // true
"hello" != "hola";   // true

1 != 1;              // false
"hello" != "hello";  // false</pre>

<h3 id="与类型转换比较">与类型转换比较</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="对象比较">对象比较</h3>

<pre class="brush: js notranslate">const object1 = {"key": "value"}
const object2 = {"key": "value"};

object1 != object2 // true
object2 != object2 // false</pre>

<h2 id="规范">规范</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="浏览器兼容性">浏览器兼容性</h2>



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

<h2 id="参见">参见</h2>

<ul>
 <li><a href="/zh-CN/docs/Web/JavaScript/Reference/Operators/Equality">Equality operator</a></li>
 <li><a href="/zh-CN/docs/Web/JavaScript/Reference/Operators/Strict_equality">Strict equality operator</a></li>
 <li><a href="/zh-CN/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>