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?
return !!(window.Object && (obj1.function1 || obj.2function2)));
It extends the whole "forcing the conversion" on all the things. convertallthethings.jpeg – Potboy