3
function F() {
    w = !0;
    return !1
}

Is this a special way of setting a boolean variable? Why did the author do this?

Edit: Context:

document.onmousedown = F;
Leo Jiang
  • 24,497
  • 49
  • 154
  • 284
  • 1
    I don't recommend this approach as it's entirely unnecessary to negate hard coded values. – TGH Oct 02 '13 at 02:35
  • @TGH `!0` is `true` (a boolean). Note that while `0 == false, 0 !== false` – SheetJS Oct 02 '13 at 02:37
  • @TGH I think the point of `!0` is to save two characters (`"!0".length` is 2 while `"true".length` is 4) – SheetJS Oct 02 '13 at 02:40
  • Why did someone downvote this? Was a legit question. (Returned it to 0.) – Jackson Oct 02 '13 at 02:40
  • 3
    @Jackson: Most likely that person downvoted the question because they thought that the questioner did not put enough effort into researching the question. Anyway, seems to be a duplicate. – Qantas 94 Heavy Oct 02 '13 at 02:54
  • 3
    Someone's trolling through and downvoting everything anonymously. Such is SO. – kojiro Oct 02 '13 at 02:54
  • @Nirk returning a true/false as boolean should not take up any more space than the !0 approach. It wouldn't return "true" (4 chars) in any event unless a "true" string was hard coded. I guess there is a very minor difference in script size as you it would let you download less script code (less characters), but the memory footprint should be the same once downloaded – TGH Oct 02 '13 at 03:36
  • Darn duplicates are always ruining the fun. – Jackson Oct 02 '13 at 03:37
  • @TGH the point is that the size of the **source code** when you write `!0` is smaller compared to `true`. That means less code needs to be sent over the network. – SheetJS Oct 02 '13 at 06:34

7 Answers7

2

w is a variable from an outer scope. !0 is true but only takes 2 bytes, so my guess is that the author wanted to set w to be true in the callback function and wanted to save bytes.

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
SheetJS
  • 22,470
  • 12
  • 65
  • 75
2

This is an example of unnecessary prowess. The code below is exactly the same:

function F() {
    w = true; //w = !0;
    return false //return !1
}

If you are using a good minification tool, which you should in production, clever programming to save a few bytes becomes unnecessary.

stavarotti
  • 582
  • 5
  • 19
1

The exclamation mark is a logical not operator that will convert these values to boolean and ensure boolean types

so w is assigned true

and your return is false

zoranc
  • 2,410
  • 1
  • 21
  • 34
0

In Javascript 0 is a false value, and any non-zero value is true. The ! is a logical 'not', so !0 is 'not false', or 'true', while !1 is 'not true', or 'false'.

As to why the author did this - I have no idea.

0

! is the negation operator. !0 is true. !1 is false.

SheetJS
  • 22,470
  • 12
  • 65
  • 75
Will
  • 2,343
  • 1
  • 14
  • 14
0

Yes, it is a way to set a boolean. In this case the function will return false, because 1 can be evaluated to true and therefore its negation (!) would evaluate to false.

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
Fabio
  • 791
  • 1
  • 7
  • 27
-1

! means "not".

The example you show doesn't make much sense, generally where you would use this is something like:

var visible = false;

$('#link').click(function () {
   visible = !visible;
});

In this case above, each click would 'toggle' the variable visible.

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
aaronfay
  • 1,673
  • 1
  • 17
  • 19