The conditional operator ? :
The conditional operator picks one of two values based on a condition: condition ? valueIfTrue : valueIfFalse.
The conditional (ternary) operator is an expression with three parts: condition ? a : b. JavaScript evaluates the condition. If it is truthy, the result is a. Otherwise it is b.
Because it is an expression, it produces a value. That is why you can use it after return, in a const assignment, or inside a template literal. An if is a statement and produces no value.
In the course you see it like this: return express ? base * 2 : base;. Read it as: "if express is true, return base * 2; otherwise return base".
TypeScript gives the result the union of both sides' types. If one side is a string and the other a number, the type is string | number. Those types are gone when Node runs the code.
Use a ternary for short decisions. If it nests or grows long, an if reads better.
Reach for it when: you need to choose between two values in one place, such as when returning, assigning or building a text.
Examples
Common mistake
const n: number = 5; const r: string = n > 3 ? "big" : 0; console.log(r);
Why: The result has type `"big" | 0`, and `0` does not fit in `string`.
How to write it: Make both sides the same type, for example "small" instead of 0.
