Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript minification why is false replaced with !1 and true with !0 [duplicate]

Tags:

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?

like image 811
Arek S Avatar asked Dec 13 '13 13:12

Arek S


People also ask

How does JavaScript minification work?

How Minification Works. Minification works by analyzing and rewriting the text-based parts of a website to reduce its overall file size. Minification extends to scripts, style sheets, and other components that the web browser uses to render the site. Minification is performed on the web server before a response is sent ...

What is the point of minification?

Minification is the process of minimizing code and markup in your web pages and script files. It's one of the main methods used to reduce load times and bandwidth usage on websites. Minification dramatically improves site speed and accessibility, directly translating into a better user experience.

Should you minify JavaScript?

It is important to minify your CSS and minimise JavaScript files so they can load faster on your web pages. There are many reasons why you should minify your CSS and JavaScript: Reduce file size: The more code there is in a file, the larger it will be. Minified code is usually much smaller than the original version.

What is the difference between minify and uglify?

Minification is just removing unnecesary whitespace and redundant / optional tokens like curlys and semicolons, and can be reversed by using a linter. Uglification is the act of transforming the code into an "unreadable" form, that is, renaming variables/functions to hide the original intent...


2 Answers

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.

like image 150
Pierre Avatar answered Oct 20 '22 04:10

Pierre


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.

like image 22
Jason Avatar answered Oct 20 '22 04:10

Jason