- Normal style:
result = (a !== null && a !== undefined) ? a : b;
- Compact style:
result = a ?? b
The result of a ?? b is:
- if a is defined, then a,
- if a isn’t defined, then b.
- The OR operator
result = a || b
When a || b equals to a ?? b
Ex:
let height = 0;
alert(height || 100); // 100
alert(height ?? 100); // 0
let height = null;
let width = null;
let area1 = height ?? 100 * width ?? 50;
let area2 = (height ?? 100) * (width ?? 50);
let area3 = height ?? (100 * width) ?? 50;