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;
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;
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.
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.
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
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.
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
.
!
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
.