1condition ? doThisIfTrue : doThisIfFalse
2
31 > 2 ? console.log(true) : console.log(false)
4// returns false
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"
1let showme || "if the variable showme has nothing inside show this string";
2let string = condition ? 'true' : 'false'; // if condition is more than one enclose in brackets
3let condition && 'show this string if condition is true';
1let amount = 50;
2let food = amount > 100 ? 'buy coka-cola' : 'buy just a water bottle';
3console.log(food)
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}
1FullName: (obj.FirstName && obj.LastName) ? obj.FirstName + " " + obj.LastName : "missing values",