diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/javascript/reference/operators/inequality | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/zh-cn/web/javascript/reference/operators/inequality')
-rw-r--r-- | files/zh-cn/web/javascript/reference/operators/inequality/index.html | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/operators/inequality/index.html b/files/zh-cn/web/javascript/reference/operators/inequality/index.html new file mode 100644 index 0000000000..2c0bccddfd --- /dev/null +++ b/files/zh-cn/web/javascript/reference/operators/inequality/index.html @@ -0,0 +1,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> + +<div class="hidden"><font><font>此页面上的兼容性表是根据结构化数据生成的。</font><font>如果您想提供数据,请查看</font></font><a href="https://github.com/mdn/browser-compat-data"><font><font>https://github.com/mdn/browser-compat-data</font></font></a><font><font>并向我们发送请求请求。</font></font></div> + +<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> |