3 ms·
It seems rare to make use of the distinction between null and undefined in real Javascript code. It mainly seems useful in debugging: seeing a crash with an und
by tlb 3y ago
It seems rare to make use of the distinction between null and undefined in real Javascript code. It mainly seems useful in debugging: seeing a crash with an undefined value tells you something different than a null value.
- sgbeal 3y ago> It seems rare to make use of the distinction between null and undefined in real Javascript code. It is admittedly rare, but when it's useful, it's _really_ useful. It can be used, e.g., to distinguish between "property is not set" and "property has been explicitly set to a non-value (null)". That's not _often_ useful, but when it is, it _really_ is. YMMV.
- yen223 3y agoWhen defining payloads for updates, it is really useful to be able to distinguish something like {"name": null, "age": 25} // Set name to null, and age to 25 from {"age": 25} // Only set age to 25, leave name alone In the former, "name" has value `null`; in the latter, the "name" field has value `undefined` Most strongly-typed mainstream backend languages don't make this distinction between "a field having value null" and "a field not being present", and this results in those languages parsing both payloads as the same. This makes API design much harder than it needs to be.
- giaour 3y agoThis is really only true if your serialization format allows you to supply an explicit `null`, omit arbitrary properties, and has no representation corresponding to `undefined`. In practice, only JSON (and serialization formats explicitly designed to be isomorphic to JSON) exhibit these characteristics.