Skip to content

Commit fd728e9

Browse files
committed
More double negation
1 parent 71eaf6b commit fd728e9

1 file changed

Lines changed: 11 additions & 5 deletions

File tree

pages/Types.html

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@
2424
</code></pre>
2525
<p>In this case, <code>"x defaulted to false"</code> is printed.
2626
</p>
27-
<p>To keep the examples short, the invert-operator is used to show a boolean context:
27+
<p>To keep the examples short, the invert ("not") operator and double-negation are used to show a boolean context:
2828
</p>
29-
<pre><code data-lang="javascript">!x // true
29+
<pre><code data-lang="javascript">
30+
var x = "";
31+
!x // true
32+
!!x // false (Double negation: Since "not (empty string)" is true, negating that makes it false)
3033
</code></pre>
3134
<p>On to the actual types.
3235
</p>
@@ -125,6 +128,7 @@ <h3 id="Boolean_Default"> Boolean Default </h3>
125128
<p>An empty string defaults to false:
126129
</p>
127130
<pre><code data-lang="javascript">!"" // true
131+
!!"" // false
128132
!"hello" // false
129133
!"true" // false
130134
!new Boolean( false ) // false
@@ -144,7 +148,7 @@ <h3 id="Boolean_Default_2"> Boolean Default </h3>
144148
<p>If a number is zero, it defaults to false:
145149
</p>
146150
<pre><code data-lang="javascript">!0 // true
147-
!!0 // false (Double negation: Since "not 0" is true, negating that makes it false)
151+
!!0 // false
148152
!1 // false
149153
!-1 // false
150154
</code></pre>
@@ -217,8 +221,8 @@ <h3 id="Float"> Float </h3>
217221
<h2 id="Boolean"> Boolean </h2>
218222
<p>A boolean in JavaScript can be either true or false:
219223
</p>
220-
<pre><code data-lang="javascript">if ( true ) console.log( "always!" )
221-
if ( false ) console.log( "never!" )
224+
<pre><code data-lang="javascript">if ( true ) console.log( "always!" );
225+
if ( false ) console.log( "never!" );
222226
</code></pre>
223227
<h2 id="Object"> Object </h2>
224228
<p>Everything in JavaScript is an object, though some are more objective (haha). The easiest way to create an object is the object literal:
@@ -281,6 +285,7 @@ <h3 id="Boolean_default_3"> Boolean default </h3>
281285
<p>An object, no matter if it has properties or not, never defaults to false:
282286
</p>
283287
<pre><code data-lang="javascript">!{} // false
288+
!!{} // true
284289
</code></pre>
285290
<h3 id="Prototype"> Prototype </h3>
286291
<p>All objects have a prototype property. Whenever the interpreter looks for a property, it also checks the prototype. jQuery uses that extensively to add methods to jQuery instances.
@@ -369,6 +374,7 @@ <h3 id="Boolean_Default_4"> Boolean Default </h3>
369374
<p>An array, no matter if it has elements or not, never defaults to false:
370375
</p>
371376
<pre><code data-lang="javascript">![] // false
377+
!![] // true
372378
</code></pre>
373379
<h3 id="Array.3CType.3E_Notation"> Array&lt;Type&gt; Notation </h3>
374380
<p>In the jQuery API you'll often find the notation of Array&lt;Type&gt;:

0 commit comments

Comments
 (0)