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"
1// ternary operators are frequently used as a shorter cleaner if statement
2// condition ? exprIfTrue : exprIfFalse
3
4let age = 15;
5let canDrive = age >= 16 ? 'yes' : 'no';
6// canDrive will be 'no'
7// the condition will be age > 16 which is false so canDrive will equal exprIfFalse
8
9// this ternary is the same as this if else statement
10let age = 15;
11let canDrive;
12if (age >= 16) {
13 canDrive = 'yes';
14} else {
15 canDrive = 'no';
16}
17
18// canDrive will be 'no' because 15 is less than 16
19
20