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');
Related
This question already has answers here:
Falsey values in JavaScript
(6 answers)
Convert truthy or falsy to an explicit boolean, i.e. to True or False
(2 answers)
Closed 3 years ago.
While programming with javascript, I come in some situations that I have a condition an wonder what values will pass that condition (if it will be truthy).
The solution I came up with is open the console in chrome and type if(foo) {true} and if the foo is truthy, it returns true, otherwise false.
An example of that is when I have some expression (foo) that won't return only true/false values. Inside the if it can return alot of things depending on the input (sometimes string, or number or maybe NaN).
But I fell that writing a if for that is too much.
Is there a easiest way of checking the truthy of a value in javascript?
Edit:
I'm looking for the easiest way of checking the truthy of a value, not just how to check and as I said in my question, I already do if(foo) {true} to check, but I'm looking for a easiest way
I would use a ternary operator to shorthand the if-statement.
The result is something like this:
const foo = true;
console.log(foo ? 'truthy' : 'falsy'); // returns truthy
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 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')}