Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use the double not (!!) operator in JavaScript

I understand what the double not operator does in JavaScript. I'm curious about it's use though and whether or not a recent assertion that I made is correct.

I said that if (!!someVar) is never meaningful nor is (!!someVar && ... because both the if and the && will cause someVar to be evaluated as a boolean so the !! is superfluous.

In fact, the only time that I could think of that it would be legitimate to use the double not operator is if you wanted to do a strict comparison to another boolean value (so maybe in return value that expects true or false explicitly).

Is this correct? I started to doubt myself when I noticed jQuery 1.3.2 used both if (!!someVar) and return !!someVar && ...

Does the double not have any actual effect in these situations?

My personal opinion is that it just leads to confusion. If I see an if statement, I know it's evaluating it as a boolean.

like image 906
Keith Bentrup Avatar asked Feb 01 '10 02:02

Keith Bentrup


People also ask

Why we use double Not operator in JavaScript?

The double negation(!!) operator calculates the truth value of a value. This operator returns a boolean value, which depends on the truthiness of the given expression.

When would you use the NOT operator?

In Boolean algebra, the NOT operator is a Boolean operator that returns TRUE or 1 when the operand is FALSE or 0, and returns FALSE or 0 when the operand is TRUE or 1. Essentially, the operator reverses the logical value associated with the expression on which it operates.

What is the use of double exclamation in JavaScript?

If you have ever noticed a double exclamation mark (!!) in someone's JavaScript code you may be curious what it's for and what it does. It's really simple: it's short way to cast a variable to be a boolean (true or false) value.

What does double negation mean in JavaScript?

Double-negation turns a "truthy" or "falsy" value into a Boolean value, true or false .


1 Answers

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.

like image 50
Christian C. Salvadó Avatar answered Sep 29 '22 03:09

Christian C. Salvadó