Saturday, July 4, 2015

the question mark operator in JavaScript

Rewriting my double ampersand example like so:

var four = 4;
var five = 5;
var whatever = four ? four : five;
alert(whatever);

 
 

...it's going to give us a 4. Basically this says, if four is truthy use four and otherwise use five, so if we instead set four to null at the first line we will get 5. Get it?

No comments:

Post a Comment