1//ternary operator syntax and usage:
2condition ? doThisIfTrue : doThisIfFalse
3
4//Simple example:
5let num1 = 1;
6let num2 = 2;
7num1 < num2 ? console.log("True") : console.log("False");
8// => "True"
9
10//Reverse it with greater than ( > ):
11num1 > num2 ? console.log("True") : console.log("False");
12// => "False"
1var variable;
2if (condition)
3 variable = "something";
4else
5 variable = "something else";
6
7//is the same as:
8
9var variable = condition ? "something" : "something else";
1// Simple (if)
2condition ? true : false
3
4
5// complex (else if)
6function example(…) {
7 return condition1 ? value1
8 : condition2 ? value2
9 : condition3 ? value3
10 : value4;
11}
12
13// Equivalent to:
14
15function example(…) {
16 if (condition1) { return value1; }
17 else if (condition2) { return value2; }
18 else if (condition3) { return value3; }
19 else { return value4; }
20}