Double Negation (!!) in JavaScript - What Is the Purpose

Double negation (!!) in JavaScript - what is the purpose?

It casts to boolean. The first ! negates it once, converting values like so:

  • undefined to true
  • null to true
  • +0 to true
  • -0 to true
  • '' to true
  • NaN to true
  • false to true
  • 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.

Can someone explain this 'double negative' trick?

A logical NOT operator ! converts a value to a boolean that is the opposite of its logical value.

The second ! converts the previous boolean result back to the boolean representation of its original logical value.

From these docs for the Logical NOT operator:

Returns false if its single operand can be converted to true; otherwise, returns true.

So if getContext gives you a "falsey" value, the !! will make it return the boolean value false. Otherwise it will return true.

The "falsey" values are:

  • false
  • NaN
  • undefined
  • null
  • "" (empty string)
  • 0

Using double negation - For what reason?

The double negation operator is used to convert as boolean.. think of it as a similar method to parseInt()

Here's a related stackoverflow question

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.

Any specific use of double negation (!!) before new?

In this case, it is indeed superfluous. Using the ! operator twice just casts a value to boolean.

However, the code you read in that bug report is not complete (like a typo). It omitted an important part of what was actually meant. If you check the commit that was made, the picture looks different:

function test() {
try {
return !!new Blob();
} catch (e) {
return false;
}
}

es6 meaning of double exclamation marks

It's a way of casting to a boolean with double negation.

A singular ! negation produces a true/false value, but it's the opposite of what you want. Double negation produces a true/false value which matches the original intent.

Try it:

!!0 // false
!!1 // true
!!"test" // true
!!null // false

JavaScript: what is the meaning of !!?

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)


Related Topics



Leave a reply



Submit