if else questionmark operators

A neat wee time saving way to write an if () {} else {} statement in flash or javascript:

var a = true;
var b = false;
var c;
if(a==b?c = true: c = false);
trace(c);

The statements between the ? and the : are if the if evaluated to true, and after the : is for false. Ie. it is this:

if(a == b) {
  c = true;
} else {
  c = false;
}

and if you’re just going for an if, no else, just throw a null in the 2nd assignment

if(a==b?b=c:null);
Tech Reference: ,

Leave a Reply

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

Proudly powered by WordPress | Theme: Baskerville 2 by Anders Noren.

Up ↑