JavaScript Ternary Conditional

12.30.2014

The ternary conditional is a shorter syntax for conditionals.

var isThor = false;
var isArcher = true;
var weapon;
if(isThor){
  weapon = "Mjolnir";
} else {
  weapon = "Longsword";
}

is the same as

var weapon = isThor ? "Mjolnir" : "Longsword";

Ternaries can be nested.

isThor ? weapon = "Mjolnir" : isArcher ? weapon = "Bow" : weapon = "Sword";