Shorthand IF statement

A quick shorthand/ single line IF statement

Say if I want to code an IF ELSE statement like this:

if (x>1){
var str = 'x is more than 1';
var str = 'x is less than 1';

Alternatively, I can go as a single line like this:

var str = (x>1) ? 'x is more than 1' : 'x is less than 1';

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>