This site runs best with JavaScript enabled.

Ternary notation

Kevin Beck

June 11, 2019


Ternary notation for conditions is something I regularly use but also regularly get mixed up. So, instead of the following:

var something;
if(x < 200){
something = 1;
} else {
something = 0;
}

...You could write a shorter version using the ternary notation:

var something = x < 200 ? 1 : 0;

The true case of the condition is after the question mark, and the other case follows the colon.

Source