开发者

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

开发者 https://www.devze.com 2023-04-06 03:48 出处:网络
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

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:

开发者_Python百科
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?


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.


!!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.)


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)


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...

0

精彩评论

暂无评论...
验证码 换一张
取 消