Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

javascript

I noticed that Closure Compiler compiles true and false (or 1 and 0) as !0 and !1. This doesn't make sense to me since it's twice as many characters as 1 and 0. Is there a reason for this? Is there any benefit?

Thanks.

like image 453
Suffick Avatar asked Mar 16 '13 07:03

Suffick


People also ask

Which is the greatest number 0 or 1?

We can clearly say that 1 is the smallest natural number and 0 is the smallest whole number. But there is no largest whole number because each number has its successor.

Which one is higher or 0?

Numbers can be positive or negative. Positive numbers are greater than 0, and negative numbers are less than 0.

What is the difference between 0 1 and 0?

And in this sense, there is also no difference between the expressions 1/0 = 1 · 0⁻¹ and 0/0 = 0 · 0⁻¹ because 0⁻¹ simply doesn't exist.

Is 1 greater than or less than 0?

tl;dr By axioms, any natural number can be obtained by starting with 0 and adding 1 to it a certain amount of times so 1 can't be less than 0 and it certainly isn't equal, ergo it is greater than.


1 Answers

1 !== true and 0 !== false, but !0 === true and !1 === false. The compiler just makes sure the type stays boolean.

Consider this 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 conditional would be false and the second true. Using var a = !0; the script would still work correctly.

like image 91
JJJ Avatar answered Nov 03 '22 00:11

JJJ