The Universal Fallback
HardAcc. 45.1%
+60 XP 25
The NaN/Null Predator
We've used || for falsy values and ?? for nullish values. But what if a value is NaN (falsy) but not nullish? ?? will not catch NaN!
A "Universal Fallback" chains these operators to ensure that no matter what goes wrong (null, undefined, or a failed math operation resulting in NaN), you always return a valid number.
The Assignment
Your function receives a parameter val.
- If
valis strictlynullorundefined, return the number0. - If
valis aNaNresult, return the number0. - Use a single logical expression (no
ifstatements) combining??and||to handle all three safely.
01EXAMPLE 1
Input
val = NaNOutput
0Explanation: NaN is caught and turned to 0.
Constraints
- Use both ?? and || in the same line.
- Return 0 for null, undefined, or NaN.
LogicShort-CircuitProfessional
JavaScriptSystem handles I/O — write your function only
Loading...
3 Hidden
Input Arguments
valnull
Expected Output
0
Click RUN to test your solution