JS double exclamation -- is there any good reason to use it?
Asked Answered
W

4

7

I've been debating this topic with a co-worker for about a week. I'm very much a fan of shorthand code, using ternaries, etc., wherever I can. Lately, he's been picking on me about my use of double exclamations. After running numerous tests, I'm beginning to agree with him... double exclamations may not be wise to use in my code. Consider this:

var myvar = "Hello";
return (!!myvar ? "Var is set" : "Var is not set");

The above example works as expected. However, if we are checking against a variable that may return undefined, we get an error, especially in IE7. We get our expected result, however, if we run this in our console:

if(randomvar) alert('Works');

Using this approach, if the variable is undefined, it fails silently. This makes me question the use of double exclamations altogether. Is there a situation that actually makes this operator beneficial?

Wearproof answered 19/9, 2011 at 20:12 Comment(0)
P
10

There is a valid use for !! in javascript. It's an expression which will take a value and convert to either the boolean true or false. It essentially coerces the current state into a boolean.

This is good for both

  1. Capturing the truthiness of the value
  2. Freeing up the original object for collection (should that be the final reference)
  3. Helps prevent later incorrect usages of an object with coercing equality (==). Doesn't prevent them all but forcing it down to a bool removes a set of scenarios.
Penta answered 19/9, 2011 at 20:16 Comment(1)
(Upvote for using the word "truthiness"! ) Great explanation, it's also worth noting that when you use this operator like this: return !!(window.Object && (obj1.function1 || obj.2function2))); It extends the whole "forcing the conversion" on all the things. convertallthethings.jpegPotboy
R
6

!!x coerces x to a boolean, with the properties x == !!x and typeof !!x === "boolean". (Might not be true in general if you override valueOf, but I don't want to think about it at the moment.)

Rigmarole answered 19/9, 2011 at 20:16 Comment(0)
W
1

So, I understand that typeof !!x === "boolean" and the logic behind it (!x casts as inverted boolean, !!x inverts back, etc), but I'm wondering if it's problematic by nature? If you cast an undefined variable, you end up with a script error on your hands. I would expect that !!x == false if typeof !!x === "undefined", but that is not the case. It seems that this would be the safer choice:

if((typeof x != "undefined") && (x != null)) { ... }

Can someone provide a scenario where using !!x is more appropriate? At the moment, I am not seeing a benefit to using it.

Scratching my head...

Wearproof answered 20/9, 2011 at 15:40 Comment(0)
S
1

I tested in IE - !!undefined returns false. Which I believe a correct behavior.

As for where !! is (or might be) useful.

Here's an example:

Suppose you have a function that accepts a parameter:

function test(param) { if (param === true) { alert('OK'); } }

Calling with: test(1) will not popup the alert window - although it is generally accepted that 1 is a true or a true-ish value. Yes someone could argue it is up to the agreement in the team and each of the developer responsibility to pass the currect data type - but what is the correct data type in JavaScript ? Especially when you don't have a compiler to verify which is which.

Thus in the situation above, I'd use if (!!param) instead of if (param === true)

Steakhouse answered 13/3, 2014 at 22:19 Comment(1)
!!param and param === true are not the same; !!param will return false for every "falsy" value, false, undefined, NaN) whereas param === true will only return true if param is of boolean type, and is set to 'true'. Like you said, if the team agrees that the type should be Boolean, don't pass it other types. I personally use the convention of naming the parameters something related to their type when passing them into functions, if it requires a strict type, just as a reminder. Like function chocolateStarfish(xInt, yBool){//stuff}; etc.Potboy

© 2022 - 2024 — McMap. All rights reserved.