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
.
Double negation (!!) in JavaScript - what is the purpose?
It casts to boolean. The first !
negates it once, converting values like so:
undefined
totrue
null
totrue
+0
totrue
-0
totrue
''
totrue
NaN
totrue
false
totrue
- All other expressions to
false
Then the other !
negates it again. A concise cast to boolean, exactly equivalent to ToBoolean simply because !
is defined as its negation. It’s unnecessary here, though, because it’s only used as the condition of the conditional operator, which will determine truthiness in the same way.
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
Is it necessary to use double exclamation points (!!) in order to make sure that an object is not null?
What you're doing is already done by JavaScript natively.
if( value ) {
}
will evaluate to true as long as value
is not:
- null
- undefined
- NaN
- empty string ("")
- 0
- false
Check out the toboolean conversions in the specification.
double NOT operator in Javascript Confusion (using it inside Vuejs instance)
The way you are accessing it is how you would if it is a variable.
Even if you do access it like an object i.e
let obj = {
a: "this is A",
b: obj.a
}
it won't work.
You are currently defining what obj
is. How can you get a value you are currently trying to define?
That's just like doing the following:
let a = a
You can however just initialize first and set right after
let obj = { a: "this is A" }
obj.b = obj.a
or a getter which may/may not be the right answer
let obj = {
a: "this is A",
get b() {
return this.a
}
}
That will permanently reflect value of obj.a
and not just on initialization.
If you're using vue, i suggest just sticking with computed
.
Double not (!!) vs type coercion in JavaScript
I don't really see any reason to do it in context like you present. It will not affect the result in any way.
The only time I'd see this as a good idea is if you're building a function which is supposed to return a bool value, and you need to cast it from some other value, eg...
function isNotFalsy(val) { return !!val; }
The example may be a bit forced, but you get the idea. You would always want to make sure the return value is of the type the user would expect.
Is there an interest to use double boolean negation?
With the double !
you force an object to return something that is "boolean-able", even if null
or something else, that can be evaluated as bool true
or false
.
Related Topics
How to Sort/Order Keys in JavaScript Objects
Capture Click on Div Surrounding an Iframe
Copy a Variable's Value into Another
Creating an Array of Cumulative Sum in JavaScript
How to Access Dom Elements in Electron
Refused to Execute a JavaScript Script. Source Code of Script Found Within Request
JavaScript Fuzzy Search That Makes Sense
Stop Cursor from Jumping to End of Input Field in JavaScript Replace
Unsigned Integer in JavaScript
Spring Boot Controller - Upload Multipart and JSON to Dto
Passing Variables to the Next Middleware Using Next() in Express.Js
Wrapping a Set of Dom Elements Using JavaScript
Script Tag in Angular2 Template/Hook When Template Dom Is Loaded
Getting Image Dimensions Using JavaScript File API
Differencebetween Using Constructor VS State = {} to Declare State in React Component
!Function(){ }() VS (Function(){ })()
Difference Between Dot Notation and Bracket Notation in JavaScript