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: Logical NOT (!)
slug: Web/JavaScript/Reference/Operators/Logical_NOT
translation_of: Web/JavaScript/Reference/Operators/Logical_NOT
---
<div>{{jsSidebar("Operators")}}</div>
<p>The logical NOT (<code>!</code>) operator (logical complement, negation) takes truth to falsity and vice versa. It is typically used with {{jsxref("Boolean")}} (logical) values. When used with non-Boolean values, it returns <code>false</code> if its single operand can be converted to <code>true</code>; otherwise, returns <code>true</code>.</p>
<div>{{EmbedInteractiveExample("pages/js/expressions-logical-not.html", "shorter")}}</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 notranslate">!<var>expr</var>
</pre>
<h2 id="Description">Description</h2>
<p>Returns <code>false</code> if its single operand can be converted to <code>true</code>; otherwise, returns <code>true</code>.</p>
<p>If a value can be converted to <code>true</code>, the value is so-called {{Glossary("truthy")}}. If a value can be converted to <code>false</code>, the value is so-called {{Glossary("falsy")}}.</p>
<p>Examples of expressions that can be converted to false are:</p>
<ul>
<li><code>null</code>;</li>
<li><code>NaN</code>;</li>
<li><code>0</code>;</li>
<li>empty string (<code>""</code> or <code>''</code> or <code>``</code>);</li>
<li><code>undefined</code>.</li>
</ul>
<p>Even though the <code>!</code> operator can be used with operands that are not Boolean values, it can still be considered a boolean operator since its return value can always be converted to a <a href="/en-US/docs/Web/JavaScript/Data_structures#Boolean_type">boolean primitive</a>. To explicitly convert its return value (or any expression in general) to the corresponding boolean value, use a double <a href="/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators#Logical_NOT">NOT operator</a> or the {{jsxref("Global_Objects/Boolean/Boolean", "Boolean")}} constructor.</p>
<h2 id="Examples">Examples</h2>
<h3 id="Using_NOT">Using NOT</h3>
<p>The following code shows examples of the <code>!</code> (logical NOT) operator.</p>
<pre class="brush: js notranslate">n1 = !true // !t returns false
n2 = !false // !f returns true
n3 = !'' // !f returns true
n4 = !'Cat' // !t returns false</pre>
<h3 id="Double_NOT_!!">Double NOT (<code>!!</code>)</h3>
<p>It is possible to use a couple of NOT operators in series to explicitly force the conversion of any value to the corresponding <a href="/en-US/docs/Web/JavaScript/Data_structures#Boolean_type">boolean primitive</a>. The conversion is based on the "truthyness" or "falsyness" of the value (see {{Glossary("truthy")}} and {{Glossary("falsy")}}).</p>
<p>The same conversion can be done through the {{jsxref("Global_Objects/Boolean/Boolean", "Boolean")}} function.</p>
<pre class="brush: js notranslate">n1 = !!true // !!truthy returns true
n2 = !!{} // !!truthy returns true: <strong>any</strong> object is truthy...
n3 = !!(new Boolean(false)) // ...even Boolean objects with a false <em>.valueOf()</em>!
n4 = !!false // !!falsy returns false
n5 = !!"" // !!falsy returns false
n6 = !!Boolean(false) // !!falsy returns false</pre>
<h3 id="Converting_between_NOTs">Converting between NOTs</h3>
<p>The following operation involving <strong>booleans</strong>:</p>
<pre class="brush: js notranslate">!!bCondition</pre>
<p>is always equal to:</p>
<pre class="brush: js notranslate">bCondition</pre>
<h2 id="Specifications">Specifications</h2>
<table class="standard-table">
<thead>
<tr>
<th scope="col">Specification</th>
</tr>
</thead>
<tbody>
<tr>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#sec-logical-not-operator', 'Logical NOT expression')}}</td>
</tr>
</tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{Compat("javascript.operators.logical_not")}}</p>
<h2 id="See_also">See also</h2>
<ul>
<li>{{jsxref("Boolean")}}</li>
<li>{{Glossary("Truthy")}}</li>
<li>{{Glossary("Falsy")}}</li>
</ul>
<p>
<audio class="hidden"></audio>
</p>
|