Skip to Content

JavaScript Tips

Ternary Operator

When you have a simple if...else statement like the following:

var a = true;
if (a == true) {
 alert("True");
}
else {
 alert("False");
}

It may be best to use the ternary operator.

var a = true;
//(Test) ? (if) : (else);
(a == true) ? alert("True") : alert("False");

References: Javascript (JS) Tutorial - Ternary Operators code, syntax ccondition, JavaScript Ant - The Conditional Operator (Ternary Operator)

Tags: