This question already has answers here:
return !1 in javascript
(3 answers)
Closed 6 years ago.
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;
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.
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.
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.
! is the negation operator. !0 is true. !1 is false.
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.
Related
This question already has answers here:
What is the !! (not not) operator in JavaScript?
(42 answers)
Can someone explain this 'double negative' trick? [duplicate]
(9 answers)
Closed 8 years ago.
I just ran into this
var strict = !! argStrict;
I can't help but wonder, what is the meaning of !!? Is it a double negative? Seems pretty redundant, not likely that the guys from php.js would use that in such case?
source: http://phpjs.org/functions/in_array/
It forces the type to become a true boolean value rather than a "truthy" value.
Examples:
var a = (1 === true) // comes out as false because 1 and true are different types and not exactly the same
var b = ((!!1) === true) // comes out as true because the 1 is first converted to a boolean value by means of negation (becomes false) and then negated a second time (becomes true)
It means basically "convert to boolean".
It's negating it twice, so it argStrict is "falsy", then !argStrict is true, and !!argStrict is false.
It returns a boolean value. false for undefined, null, 0, '' and true any truthy value.
This is an example of slacker parsing.
If you have a variable, for example a string, and you want to convert it into a boolean, you could do this:
if (myString) {
result = true;
}
This says, if myString is NOT undefined, null, empty, the number 0, the boolean false then set my string to the boolean value to true...
But it is faster, and way cooler to double bang it:
result = !! myString;
Other examples include....
//Convert to number
result = +myString;
//Bang Bang Wiggle - coerces an indexOf check into a boolean
result !!~myString.indexOf('e');
This question already has answers here:
What is "x && foo()"?
(5 answers)
Closed 8 years ago.
Let's say we have this function:
loadView : function(view) {
this.view && this.view.remove();
this.view = view;
}
What does the first line do? For example if the line was this.view && this.otherView, it would return true if both view and otherView exists and false if one of them doesn't, but now there is a function being called at the end, which is confusing me.
Is the first line equivalent to:
if(this.view) {this.view.remove()}
?
it's a short circuit evaluation.
If this.view is "falsey" then this.view.remove() will not be evaluated.
So the answer to your question is yes.
It's called a guard operator. You would usually use && in this way
if(thing1 === 1 && thing2 === 2){}
You'll notice we want to check if BOTH things return true. Well the way it works is that we know that 'thing 2 === 0' will never run if the first expression (thing1 === 1) doesn't evaluate to true. Why run the second expression if they both have to be true and the first one already failed. Taking that knowledge we can now use it as a guard to not run the second expression, unless the first expression it true, or in this case truthy.
So in your code, this.view.remove() only runs if this.view is truthy.
This question already has answers here:
return !1 in javascript
(3 answers)
Closed 9 years ago.
I've seen a explanation for something similar to b = !b. But I'm not understanding it well enough to translate over to this usage.
What does
var a = !1;
do?
a = !1 is a shorthand way of writing a = false. This is normally used when trying to compress (minify) JavaScript because it saves three bytes.
If you're seeing this in ordinary un-minified JS, then someone is probably being either lazy or obfuscatory.
Run this in chrome dev tools and see what you get.
a evaluates to false because 1 is a truthy value in javascript and therefore negating it produces false
Maybe read this http://james.padolsey.com/javascript/truthy-falsey/ . It's quite interesting :)
In general the ! will invert the boolean value of its operand.
So !a will be true if a is false or it will be false if a is true.
Hope that helps :)
! is a not operator. Therefore ! true is equal to false. It's result will be either true or false
All values in JavaScript are either "truthy" or "falsy". This describes their interpretation in contexts where a boolean (true or false) is expected.
Examples of "truthy" values: true, 1, [], {}, "text"
Examples of "falsy" values: false, 0, ""
!1 is a negation of a truthy value, which will evalute to false. b = !b is a toggler, it will change the value from a truthy to a falsy, and vice versa.
The ! operator is known as the Logical NOT operator.
In short, it returns false if the following value is 'truthy', and true otherwise.
Since 1 is 'truthy', your example, !1 reads NOT 1, which will return false.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What is the !! operator in JavaScript?
What is the difference between these two operators? Does !! have special meaning, or does it simply mean you are doing two '!' operations. I know there are "Truth" and "Truthy" concepts in Javascript, but I'm not sure if !! is meant for "Truth"
!! is just double !
!true // -> false
!!true // -> true
!! is a common way to cast something to boolean value
!!{} // -> true
!!null // -> false
Writing !! is a common way of converting a "truthy" or "falsey" variable into a genuine boolean value.
For example:
var foo = null;
if (!!foo === true) {
// Code if foo was "truthy"
}
After the first ! is applied to foo, the value returned is true. Notting that value again makes it false, meaning the code inside the if block is not entered.
This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
myVar = !!someOtherVar
What does the !! operator (double exclamation point) mean in JavaScript?
Came across this line of code
strict = !!argStrict
... here and wondered what effect the !! has on the line? Pretty new to JS!
It converts your value to a boolean type:
var x = '1';
var y = !!x;
// (typeof y === 'boolean')
Also note the following:
var x = 0;
var y = '0'; // non empty string is truthy
var z = '';
console.log(!!x); // false
console.log(!!y); // true
console.log(!!z); // false
It converts the value to a value of the boolean type by negating it twice. It's used when you want to make sure that a value is a boolean value, and not a value of another type.
In JS everything that deals with booleans accepts values of other types, and some can even return non-booleans (for instance, || and &&). ! however always returns a boolean value so it can be used to convert things to boolean.
It is a pair of logical not operators.
It converts a falsey value (such as 0 or false) to true and then false and a truthy value (such as true or "hello") to false and then true.
The net result is you get a boolean version of whatever the value is.
It converts to boolean
Its a "not not" arg
commonly used to convert (shortcut) string values to bool
like this..
if(!!'true') { alert('its true')}