4 ms·
It's funny, I have a policy of the opposite. Use undefined, don't propagate null. map.get("missingKey") returns undefined. The new ?. operator returns undefin
by mantap 6y ago
It's funny, I have a policy of the opposite. Use undefined, don't propagate null.
map.get("missingKey") returns undefined.
The new ?. operator returns undefined, even if you pass it null.
Undefined means that you don't have to care about the difference between { foo: undefined } and { }.
The only pain point is that JSON.parse() produces null, but this is relatively easy to work around.
I don't find any reason to ever produce a null. I just treat it as a value that can occur when using certain APIs.
If your code depends on the difference between a missing key and a missing value, probably you should be using a Map which makes that difference explicit with separate set() and delete() methods.