Write a function `getValueOrDefault(val, defaultVal)` that returns `val` if it is NOT null/undefined, otherwise returns `defaultVal`. Note: `0`, `""`, and `false` should NOT trigger the default.
Problem Statement
Write a function `getValueOrDefault(val, defaultVal)` that returns `val` if it is NOT null/undefined, otherwise returns `defaultVal`. Note: `0`, `""`, and `false` should NOT trigger the default.
Complexity
Time Complexity: -
Space Complexity: -
Hints
š” Hint 1:
The || operator treats 0, "", and false as falsy. You need something different.
š” Hint 2:
The nullish coalescing operator (??) only checks for null and undefined.
š” Hint 3:
return val ?? defaultVal;
ā
Solution:
```javascript
function getValueOrDefault(val, defaultVal) {
return val ?? defaultVal;
}
```