javascript - Interpret a cumbersome conditional expression -


last night looking angularjs snippet manage vertical tabs menu , found expression:

if(+!!variablename)   dosomething(); 

sorry, can't understand mean +!!variablename how can resolve conditional expression?

this 1 way of saying if number if not null or undefined

if(+!!variablename)   dosomething(); 

if variablename 0, empty (""), null or undefined, translate false or else true.

for example, if

var variablename = null; 

now, translate (+!!null) -> (+!true) -> (+false) -> false

similarly, if

var variablename = "anything"; 

this translate (+!!"anything") -> (+!false) -> (+true) -> true

+ in case has no effect on outcome, can safely removed.

in fact, whole conditional expression can replaced by

if(variablename)   dosomething(); 

Comments

Popular posts from this blog

How to check data type in C++? -

javascript - Is getTimezoneOffset() stable during daylight saving transition? -

formula - R: how to pass in a reference to the variable in glm or lm? -