Purpose of Function returns only boolean

58 Views Asked by At

Somewhere in jQuery I have seen 2 functions that returns only boolean like below.

function h(){
   return !0;
}

function r(){
   return !1; 
}

What is the purpose of doing this, while Boolean can be written directly?

2

There are 2 best solutions below

1
Tushar On

0 is falsey and 1 is truthy.

So, there is no need to convert them to boolean unless they are used in code like

if (h() === false) {

Or code converted when the code is minified as true and false are minified to !1 and !0 respectively.

0
Damon On

Economy of characters.

Having a 3 character function that returns true or false takes less characters than doing an exact comparison.

var a = false;
if (a === r()) console.log('3 characters instead of 5')

Large libraries usually alias commonly used globals like undefined and window as well for minification.

For example, you may see something like

(function($, a, b) { ... })(jQuery, window, undefined)