What is the "!!" operator in Javascript? [duplicate] - javascript

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
What is the !! operator in JavaScript?
Sorry if this one is obvious but I can't google it.
What is the "!!" operator in Javascript? e.g.
if (!!window.EventSource) {
var source = new EventSource('stream.php');
} else {
// Result to xhr polling :(
}
Did the author just use "!" twice i.e. a double negation? I'm confused because this is in the official doc.

It will convert anything to true or false:
!!0 // => false
!!1 // => true
!!'a' // => true
!!'' // => false
!!null // => false
Technically, !! is not an operator, it is just two ! operators next to each other. But a double-negation is pointless unless you are using !! like an operator to convert to Boolean type.

In most languages, !! is double negation, as ! is negation. Consider this:
# We know that...
!false == true
# And therefore...
!!false == false
!!true == true
It's often used to check whether a value exists and is not false, as such:
!!'some string' == true
!!123 == true
!!myVar == true

!! is used to convert a non-zero/non-null value to boolean true and a zero/null value to false.
E.g. if a = 4, then !a = false and !!a = !(!a) = true.

Related

Why is !!'0' == true when '0' == false [duplicate]

This question already has answers here:
In JavaScript, why is "0" equal to false, but when tested by 'if' it is not false by itself?
(15 answers)
Closed 3 years ago.
The question says it all as I understand !!false = false
Then if
0 == false // true
'0' == false // true
why
!!0 == false
!!'0' == true
string zero ('0') is a truthy value
!!'0' == true
!'0' -> !false -> true
So you're actually doing
true == true
It looks like you stumbled across the importance of the === operator.
'0' == false; // true
'0' === false; // false
Boolean('0'); // true
typeof('0'); // string
typeof(!'0'); // boolean
!'0' === false; // true
!!'0' === false // false
The first negation converts string '0' to a boolean by calling of abstract function ToBoolean. According to JavaScript specification, only 7 values are evaluated/coerced/converted to false i.e are falsy:
null, undefined, NaN, Empty String, +0, -0, and false.
So, '0' is evaluted to a truthy, !'0' to false and !!'0' to true.
PS: Another cases of why '0' == false is evaluated to true is raised after the original question by the OP in a comment below. Even though not relevant to the original post, here is the explanation:
In the specification, section Abstract Equality Comparison reads: "When evaluating x == y, if the type of y is Boolean, first convert y to Number and then do the comparison again".
So Number(false) is evaluated to 0. In the next comparison run, string '0' is compared with number 0 i.e '0' == 0. The spec says convert the string to number and do the comparison again: 0 == 0.

Meaning of !! javascript [duplicate]

This question already has answers here:
What is the !! (not not) operator in JavaScript?
(42 answers)
Closed 7 years ago.
I found some code about authentication with angular and i can't understand this trick :
authService.isAuthenticated = function () {
return !!Session.userId;
};
What does !! mean 'different of userId' ?
whenever true = !!true = !!!!true =>etc, it don't understand this.
Somebody can help me?
(https://medium.com/opinionated-angularjs/techniques-for-authentication-in-angularjs-applications-7bbf0346acec for the source , part 'The AuthService')
!! Converts any value to a boolean value
> !!null
false
> !!true
true
> !!{}
true
> !!false
false
If a value is falsey then the result will be false. If it is truthy the result will be true.
Even more, the third ! inverts the converted value so the above examples become:
> !!!null
true
> !!!true
false
> !!!{}
false
> !!!false
true
It forces what is returned to be a boolean and not an integer or empty value. For example, 0 evaluates to false with == but will not with ===. So to be sure that any integer 0 returned will be converted into an boolean, we use !!. This also works if null or undefined is returned.
So whats happening is actually:
var test = null;
var result = !test; // returns true
result = !return; // returns false
!! is used to convert the value to the right of it to its equivalent boolean value.
!!false === false
!!true === true
Coerces oObject to boolean. If it was falsey (e.g. 0, null, undefined, etc.), it will be false, otherwise, true.
!oObject //Inverted boolean
!!oObject //Non inverted boolean so true boolean representation
So !! is not an operator, it's just the ! operator twice.
Referred from : https://stackoverflow.com/a/784946/2218635

What's the most concise way to check for an integer or float in JavaScript? [duplicate]

This question already has answers here:
Validate decimal numbers in JavaScript - IsNumeric()
(52 answers)
Closed 8 years ago.
I want a concise expression to tell me if a given value is an integer or float. When provided the input value NaN or Infinity, I want the output value to be false, so this rules out simply checking whether typeof(val) == 'number'
Desired input / output:
1 => true
-42 => true
3.14 => true
NaN => false
Infinity => false
null => false
undefined => false
isFinite() is almost perfect, but there's one gotcha: isFinite(null) returns true, when I actually want it to return false.
Short of always checking if ((val != nul) && isFinite(val)), is there another more concise technique?
// As suggested in the comments, a function:
var isNumber = function (val) {return !isNaN(parseInt(val))};
console.log(isNumber(1)) // true
console.log(isNumber(-42)) // true
console.log(isNumber(3.14)) // true
console.log(isNumber(NaN)) // false
console.log(isNumber(Infinity)) // false
console.log(isNumber(null)) // false
console.log(isNumber(undefined)) // false

What is the purpose of using !! in this situation?

see http://webmail.mac.com source.
B.incompatible = !!(B.msie < 7 || B.safari < 500 || B.mozilla < 1.008000999);
B.unsupported = !!(B.opera || B.safari < 500) && !B.incompatible;
B.supported = !B.incompatible && !B.unsupported;
Why are the double "!" used here? Is there any benefit?
There is no point in this case, since the expressions already evaluate to boolean values anyway. It's probably just a programmer being "extra sure".
The logical NOT operator ("!") is used to convert true to false and vice versa.
! true // expresses false
! false // expresses true
However, it also coerces values. Non zero numbers and non empty strings are true. So they become false.
! 1.0 // expresses false
! 0.0 // expresses true
! '#' // expresses false
! '' // expresses true
Using two NOTs converts it back to the original Boolean value.
!! 1.0 // expresses true
!! 0.0 // expresses false
!! '#' // expresses true
!! '' // expresses false
It's equivalent to calling the Boolean constructor. However, it's faster (no function call overhead), shorter, and more readable.
!! 1.0 === Boolean(1.0) // expresses true
Its a very smart way of casting to a bool.

Javascript Logical Operator:?

I was examining the src of underscore.js and discovered this:
_.isRegExp = function(obj) {
return !!(obj && obj.test && obj.exec && (obj.ignoreCase || obj.ignoreCase === false));
};
Why was "!!" used? Should it be read as NOT-NOT or is there some esoteric JS nuance going on here?
It is just an obtuse way to cast the result to a boolean.
Yes, it's NOT-NOT. It is commonly used idiom to convert a value to a boolean of equivalent truthiness.
JavaScript understands 0.0, '', null, undefined and false as falsy, and any other value (including, obviously, true) as truthy. This idiom converts all the former ones into boolean false, and all the latter ones into boolean true.
In this particular case,
a && b
will return b if both a and b are truthy;
!!(a && b)
will return true if both a and b are truthy.
The && operator returns either false or the last value in the expression:
("a" && "b") == "b"
The || operator returns the first value that evaluates to true
("a" || "b") == "a"
The ! operator returns a boolean
!"a" == false
So if you want to convert a variable to a boolean you can use !!
var myVar = "a"
!!myVar == true
myVar = undefined
!!myVar == false
etc.
It is just two ! operators next to each other. But a double-negation is pointless unless you are using !! like an operator to convert to Boolean type.
It will convert anything to true or false...

Categories

Resources