0

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?

shyammakwana.me
  • 5,562
  • 2
  • 29
  • 50

2 Answers2

3

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)
Damon
  • 4,216
  • 2
  • 17
  • 27
2

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.

Tushar
  • 85,780
  • 21
  • 159
  • 179