What is the difference between the below codes in JavaScript?
if(x)
doSomething();
and
if(!!x)
doSomething();
What is the difference between the below codes in JavaScript?
if(x)
doSomething();
and
if(!!x)
doSomething();
No logical difference - the JS runtime will automatically figure out the "truthiness" of a statement.
I've seen this in a few places before (or the even more fun if(!!!x)
instead of if(!x)
) in the past.
<opinion> I think it has something to do with older JS runtimes that did a poor job of truthiness testing. I believe this to be an obsolete and overly verbose, if valid, construct.</opinion>