javascript - Difference between if (x) { foo(); } and x ? foo() : 0; -


snippet 1:

if ( x ) {      foo(); } 

snippet 2:

x ? foo() : 0;   

what differences between 2 snippets?

edit: corrected syntax error.

update: btw, seems there shorter notation:

x && foo(); 

generally, if statement (e.g. can't assign variable) , ternary operator ?: part of expression (yields value can assigned variables).

also, if block can contain statements while components of ?: can contain expressions.

in example give, there no difference, since don't use result of ?:. both snippets evaluate foo() if x true value.


Comments

Popular posts from this blog

Add email recipient to all new Trac tickets -

400 Bad Request on Apache/PHP AddHandler wrapper -

php - Change action and image src url's with jQuery -