22

I'm writing an app using Enyo2 which comes with a minification tool based on UglifyJS. I've noticed that:

var t = false

is replaced with

var t=!1

The same way true is replaced with !0. I'm sure there is a good explanation for that, I just can't find it. Any idea?

pleasedontbelong
  • 19,542
  • 12
  • 53
  • 77
Arek S
  • 4,239
  • 4
  • 24
  • 33

4 Answers4

33

There is one apparently. If you use 1 or 0 to translate true or false, it would be shorter but seen as integers. If you add ! in front and reverse them, it will be coalesced into booleans and still be short.

Pierre
  • 853
  • 9
  • 21
19

It is the smallest non-ambiguous way to express the value.

true uses 4 characters on the wire and false uses 5 characters. !1 & !0 use 2 characters.

dashtinejad
  • 6,193
  • 4
  • 28
  • 44
Jason
  • 15,915
  • 3
  • 48
  • 72
  • So why not simply use 1 as true and 0 as false? – Arek S Dec 13 '13 at 13:23
  • 12
    Because 1 & 0 could be number values too. That's why I added non-ambiguous – Jason Dec 13 '13 at 13:24
  • @ArekS 1 and 0 aren't booleans; they're numbers. Comparisons could potentially be messed up – Ian Dec 13 '13 at 13:25
  • @Jason it would be nice if you could add that comment to the answer ;-) – Salvatorelab Dec 17 '13 at 12:58
  • 1
    why dont we simply use `const T = !0, F= !1` during minification, and use `T` and `F` everywhere instead of !0 and !1. that's gonna save even more space. – sanjeevprasad Sep 26 '18 at 05:56
  • @sanjeev I think it's because most short variable names like that would already be taken for other things and since you'd be saving at most one character per boolean literal it's probably not worth it. There are better cases to use the one letter variable names – Zachiah Oct 14 '22 at 23:29
11

In JavaScript, 0 is a falsy value. This means that 0 equates to false when represented as a boolean type. 1 on the other hand; or any other positive number for that matter, is not a falsy value and will equate to true.

Here we could just set:

t = 0; // false
t = 1; // true

The problem with this is that these values are integers (numbers) and not boolean values (true or false). Using a strict equality check (===) we'd find:

t = 0;
t == false; // true
t === false; // false

The ! operator performs a logical NOT operation on the value, and can be used as a quick way to convert any value to boolean:

t = !1;
t == false; // true
t === false; // true

Performance-wise, there is not really much difference between if (!t) and if (t == true), however in minification terms this reduces our JavaScript by 7 bytes, allowing it to be downloaded ever so slightly quicker.

James Donnelly
  • 126,410
  • 34
  • 208
  • 218
  • nice answer. but what about performances ? is it more performant to write !0 or false ? – Cyril Nov 04 '16 at 14:49
3

Negation converts a non-boolean, but truthy value to a pure boolean.

I ran this in Chrome Developer Tools:

> !1
false
> !0
true

Therefore, !1 is interchangeable with false and !0 is interchangeable with true. So now that we know it's safe to change it, the next question is why. Well, because it's fewer bytes. The point of minification is to make the code smaller, but compatible.

Long-story short, it's a safe way to compress the literal true and false so they take less space when transmitting over the wire.

Kilian Stinson
  • 2,376
  • 28
  • 33
Brandon
  • 9,822
  • 3
  • 27
  • 37