Is! 0 and! 1 is better than 1 and 0?

I noticed that the Closure Compiler compiles true and false (or 1 and 0) like! 0 and! 1. This does not make sense to me, since there are twice as many characters as 1 and 0. Is there a reason for this? Is there any benefit?

Thanks.

+7
source share
1 answer

1 !== true and 0 !== false , but !0 === true and !1 === false . The compiler simply ensures that the type remains logical.

Consider the following example:

 var a = true; if( a === true ) { console.log( 'True!' ); } if( a === 1 ) { console.log( 'You should never see this.' ); } 

If you change the first line to var a = 1; , the first condition will be false, and the second will be true. Using var a = !0; , the script will work correctly.

+10
source

All Articles