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

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -