JavaScript shorthand ternary operator

var startingNumber = startingNumber || 1;

Something like that what you’re looking for, where it defaults if undefined?

var foo = bar || 1; // 1
var bar = 2;
foo = bar || 1;     // 2

By the way, this works for a lot of scenarios, including objects:

var foo = bar || {}; // secure an object is assigned when bar is absent

Leave a Comment