4 ms·
Undefined is “stored” the same way in JSON, which means it isn’t. This only becomes a “problem” when you convert a JSON string to an object and the data contain
by anuila 6y ago
Undefined is “stored” the same way in JSON, which means it isn’t. This only becomes a “problem” when you convert a JSON string to an object and the data contains an array with a null value in it. That’s the only case where null exists in JSON, and I think it’s generally pretty rare.
- Waterluvian 6y agoNot sure I follow. JSON has null. It doesn't have undefined.
- toast0 6y agoYou said > But otherwise I just leave undefined to play the role of "this optional argument wasn't defined" And the reply is saying that's how JSON uses it too. Anything set to undefined will not be serialized, as if it were optional and not defined.
- Waterluvian 6y agoArguments belong to functions. Sorry I was wandering all over.
- 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.