What is the !! (not not) operator in JavaScript?
It converts Object
to boolean
. If it was falsey (e.g., 0
, null
, undefined
, etc.), it would be false
, otherwise, true
.
!object // Inverted Boolean
!!object // Noninverted Boolean, so true Boolean representation
So !!
is not an operator; it's just the !
operator twice.
It may be simpler to do:
Boolean(object) // Boolean
Real World Example "Test IE version":
const isIE8 = !! navigator.userAgent.match(/MSIE 8.0/);
console.log(isIE8); // Returns true or false
If you ⇒
console.log(navigator.userAgent.match(/MSIE 8.0/));
// Returns either an Array or null
But if you ⇒
console.log(!!navigator.userAgent.match(/MSIE 8.0/));
// Returns either true or false
When to use the double not (!!) operator in JavaScript
In the context of if
statements I'm with you, it is completely safe because internally, the ToBoolean
operation will be executed on the condition expression (see Step 3 on the spec).
But if you want to, lets say, return a boolean value from a function, you should ensure that the result will be actually boolean, for example:
function isFoo () {
return 0 && true;
}
console.log(isFoo()); // will show zero
typeof isFoo() == "number";
In conclusion, the Boolean Logical Operators can return an operand, and not a Boolean
result necessarily:
The Logical AND operator (&&
), will return the value of the second operand if the first is truly:
true && "foo"; // "foo"
And it will return the value of the first operand if it is by itself falsy:
NaN && "anything"; // NaN
0 && "anything"; // 0
On the other hand, the Logical OR operator (||
) will return the value of the second operand, if the first one is falsy:
false || "bar"; // "bar"
And it will return the value of the first operand if it is by itself non-falsy:
"foo" || "anything"; // "foo"
Maybe it's worth mentioning that the falsy values are: null
, undefined
, NaN
, 0
, zero-length string, and of course false
.
Anything else (that is not falsy, a Boolean
object or a Boolean
value), evaluated in boolean context, will return true
.
What is the !! (not not) operator in JavaScript?
It converts Object
to boolean
. If it was falsey (e.g., 0
, null
, undefined
, etc.), it would be false
, otherwise, true
.
!object // Inverted Boolean
!!object // Noninverted Boolean, so true Boolean representation
So !!
is not an operator; it's just the !
operator twice.
It may be simpler to do:
Boolean(object) // Boolean
Real World Example "Test IE version":
const isIE8 = !! navigator.userAgent.match(/MSIE 8.0/);
console.log(isIE8); // Returns true or false
If you ⇒
console.log(navigator.userAgent.match(/MSIE 8.0/));
// Returns either an Array or null
But if you ⇒
console.log(!!navigator.userAgent.match(/MSIE 8.0/));
// Returns either true or false
javascript: What is a NOT NOT? (!! operator )
i believe it is used for enforcing boolean types...
for example
if("true" == true){
alert("1");
}else{
if(!!"true" == true){
alert("2");
}
}
alerts 2 not 1
How does the double exclamation (!!) work in javascript?
!
is the logical negation or "not" operator. !!
is !
twice. It's a way of casting a "truthy" or "falsy" value to true
or false
, respectively. Given a boolean, !
will negate the value, i.e. !true
yields false
and vice versa. Given something other than a boolean, the value will first be converted to a boolean and then negated. For example, !undefined
will first convert undefined
to false
and then negate it, yielding true
. Applying a second !
operator (!!undefined
) yields false
, so in effect !!undefined
converts undefined
to false
.
In JavaScript, the values false
, null
, undefined
, 0
, -0
, NaN
, and ''
(empty string) are "falsy" values. All other values are "truthy."(1):7.1.2 Here's a truth table of !
and !!
applied to various values:
value │ !value │ !!value
━━━━━━━━━━━┿━━━━━━━━━━┿━━━━━━━━━━━
false │ ✔ true │ false
true │ false │ ✔ true
null │ ✔ true │ false
undefined │ ✔ true │ false
0 │ ✔ true │ false
-0 │ ✔ true │ false
1 │ false │ ✔ true
-5 │ false │ ✔ true
NaN │ ✔ true │ false
'' │ ✔ true │ false
'hello' │ false │ ✔ true
How do I use the logical NOT (!) operator when invoking functions in JS?
When you refer to a function, rather than calling it, you're referring to the function's object reference:
function foo() {
alert("Hi there");
}
var f = foo; // <== Getting the function's reference, not calling it
f(); // <== Now we call it
So !isEven
would be negating the function reference. Since isEven
is a non-null
reference, it's truthy; and so !isEven
is false
. Not what you want. :-)
Your reject
could be written with a function that calls test
and inverts its return value:
_.reject = function(collection, test) {
return _.filter(collection, function(e) { return !test(e); });
};
Or if you want to go the functional programming approach, you can write a function that, when called, will return a new function that negates the return value:
function not(f) {
return function() {
return !f.apply(this, arguments);
};
}
Then anywhere you want to invert a callback's meaning, you'd just use invert
:
_.reject = function(collection, test) {
return _.filter(collection, not(test));
};
Why use the `+` (plus) or `!` (logical not) operator in an IIFE?
In the cases of !function() {}()
and +function() {}()
JavaScript will first evaluate the right handside of the given statement/IIFE and then cast the return value of the IIFE to a boolean in case of !
or to type number in case of +
.
If the IIFE returns a boolean !
will negate the return value.
Example
console.log(
!function() {
return "something";
}()
);
/**
!function() {
return "something";
}()
evaluates to
!"something" and not true() or false()
lastly !"something" is equivalent to false
*/
Related Topics
Why Would a JavaScript Variable Start With a Dollar Sign
Attach Event to Dynamic Elements in JavaScript
Modifying a Copy of a JavaScript Object Is Causing the Original Object to Change
Resizing an Iframe Based on Content
What Is the "Right" Json Date Format
How to Detect Idle Time in JavaScript
Is There a "Null Coalescing" Operator in JavaScript
JavaScript For Detecting Browser Language Preference
Executing ≪Script≫ Elements Inserted With .Innerhtml
JavaScript Raises Syntaxerror With Data Rendered in Jinja Template
How to Add a Class to a Given Element
How to Get a Subset of a JavaScript Object'S Properties
How to Get All Properties Values of a JavaScript Object (Without Knowing the Keys)
Load and Execute Order of Scripts
Why Does Changing an Array in JavaScript Affect Copies of the Array
How to Loop Through a Plain JavaScript Object With the Objects as Members
Creating a New Dom Element from an HTML String Using Built-In Dom Methods or Prototype