3 ms·
> Undefined is “stored” the same way in JSON, which means it isn’t. Null is: >> JSON.stringify({"foo": undefined}); "{}" >> JSON.stringify({"foo": null}
by Izkata 6y ago
> Undefined is “stored” the same way in JSON, which means it isn’t.
Null is:
>> JSON.stringify({"foo": undefined});
"{}"
>> JSON.stringify({"foo": null});
"{\"foo\":null}"
- anuila 6y agoThe point is, why use it at all if both values are equivalent to "missing"? I don't see a practical difference between undefined and null in the vast majority of cases, which is the the author is arguing. Again: In the vast majority of cases. I'm sure you'll find your exceptions.
- rbanffy 6y agoIn the first case I lost the information that the key exists and can contain a value. The second form makes it explicit. On the first case, the parser that's getting the data may want to calculate a sensible value. On the second one, it's clear the value is known.
- bpicolo 6y agoI've worked with a ton of APIs where these two aren't equivalent. Consider partial object updates. "null" means "explicitly set a field to null", whereas undefined means "don't change the value of the field". The distinction is meaningful.