Javascript ! and !! differences [duplicate] - javascript

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.

Related

Easiest way of checking the truthy of a value in javascript? [duplicate]

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

Javascript: If([]) unexpected result [duplicate]

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.

JavaScript: what is the meaning of "!!"? [duplicate]

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');

Not clear on what this variable setting does: var a = !1 [duplicate]

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.

What does a !! in JavaScript mean? [duplicate]

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')}

Categories