

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 3
click for more info
Not enough gems
Cost: 6 gems
1: Conditionals
incomplete
2: Comparison Operators
incomplete
3: Logical Operators
incomplete
4: Switch
incomplete
5: Ternary Operator
incomplete
6: When to Ternary
incomplete
7: Truthy and Falsy
incomplete
8: Nullish Coalescing
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
You will probably use if/else statements more often than ternaries. Ternary operations are great for small, single-line conditionals. I've seen developers in the professional world get... clever... with ternaries and it can lead to nested monstrosities like this:
const vehicleName = isTruck
? "truck"
: isCar
? "car"
: isScooter
? "scooter"
: "vehicle";
I mean it works, and it's all on one line, but it's much harder to understand IMO. I'd prefer to see this:
let vehicleName = "vehicle";
if (isTruck) {
vehicleName = "truck";
} else if (isCar) {
vehicleName = "car";
} else if (isScooter) {
vehicleName = "scooter";
}
or maybe a function (we'll cover functions soon):
function getVehicleName(isTruck, isCar, isScooter) {
if (isTruck) {
return "truck";
}
if (isCar) {
return "car";
}
if (isScooter) {
return "scooter";
}
return "vehicle";
}
Remember: Code is written for humans not machines.