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.
Related
This question already has answers here:
Why if([]) is validated while [] == false in javascript?
(3 answers)
Closed 6 years ago.
I think in every language I know
if(a)
is the same thing as
if(a == true)
Turns out in JavaScript it isn't true, because:
if([])
Seems to act as if the condition is fulfilled, but:
if([] == true)
Does the opposite thing.
I can't really find any possible explanation, especially that this problem doesn't occur with empty string for example (which is == true, but isn't === true, same as empty array). Is this a bug in JavaScript or what?
In JavaScript, there is a concept of truthy and falsey values. if statements test the truthiness or falsiness of a given value rather than strict equality to true or false.
true is obviously truthy. false is obviously falsey. The rest can be a little tricky. MDN has possibly the clearest documentation about which values evaluate to falsey: https://developer.mozilla.org/en-US/docs/Glossary/Truthy
In this case [] is a truthy value so the condition passes and the code is executed.
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:
Which equals operator (== vs ===) should be used in JavaScript comparisons?
(48 answers)
Closed 9 years ago.
The community reviewed whether to reopen this question last year and left it closed:
Original close reason(s) were not resolved
Can someone help me by explaining the difference. From what I understand the === does an exact match but what does this mean when comparing with null ?
What does this mean when comparing with null?
It means exactly what you already said: It checks whether the value is exactly null.
a === null is true if the value of a is null.
See The Strict Equality Comparison Algorithm in the specification:
1. If Type(x) is different from Type(y), return false.
2. If Type(x) is Undefined, return true.
3. If Type(x) is Null, return true.
So, only if Type(a) is Null, the comparison returns true.
Important: Don't confuse the internal Type function with the typeof operator. typeof null would actually return the string "object", which is more confusing than helping.
a == null is true if the value of a is null or undefined.
See The Abstract Equality Comparison Algorithm in the specification:
2. If x is null and y is undefined, return true.
3. If x is undefined and y is null, return true.
=== means it checks both the value and the type of the variables. For example pulled from the w3c page, given x = 5, x is an int, so x==="5" is false because it is comparing and int to string and x ===5 is true because it is both an int and the right value.
=== is strict operator it not only compare value, but also type of variables so
string===string
int===int
== only compare values.
Using the triple equals, the values must be equal in type as well.But not in ==.
i.e
1==true // this return true
1===true // but return false
a==null // will true if a is null or undefined
1==true will be true
but 1===true will be false
eg. === compares on data type level while using == JavaScript will typecast by it self
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.
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.