0

In Javascript, there seems to be or have been an idea that undefined represents a missing primitive OR object value, while null represents just a missing object value.

See, for example, this section in Speaking JS.

The use of null in JSON, however, does not seem to obey this principle. If the value of my key is missing, JSON represents this situation as "the key is present with the value null". But the value could have been either a primitive or an object, so wouldn't it be more correct to those semantics to use undefined in this case?

I know JSON has no particular reason to be faithful to this aspect of Javascript now, but I'm curious about the considerations that went into this decision at the time that it was made.

3

undefined means that you got the name of the variable/field wrong, or that for some reason that variable/field was not set (maybe the program did not go through the code path that sets it?). null, however, means that you got the name right, and that the code that was supposed to put a value in there did put a value - but that value legitimately means "there is nothing here".

In other words - dog.tail === undefined means that the concept of dogs having tails is meaningless. dog.tail === null means this particular dog has no tail.

  • Are you talking about the JS semantics here? I don't think the builtin JS functions consistently use undefined and null in this manner -- for example, suppose I am searching for the dog's tail in an array of tails using Array.find, and it is not present. Array.find will return undefined, but that does not mean the concept is meaningless. – Eli Rose Feb 3 at 17:11
  • You asked about JSON - a serialization format meant to used by other languages. And in these other languages, null means "this thing is empty" while undefined means "what are you even talking about?" and actually throws an exception when you try to access that field. That JS is returning undefined instead of throwing an exception is an anomaly. – Idan Arye Feb 3 at 22:23
  • The problem with nailing down the meaning of null is that so many people have used it to mean so many things. – candied_orange Feb 4 at 6:40
  • 1
    Thanks @IdanArye, just trying to understand. Are you saying that null was chosen for JSON because the token "null" is more commonly used in other languages than the token "undefined"? – Eli Rose Feb 4 at 15:29
  • It's less about the tokens null and undefined and more about the concepts represented by these tokens. Most languages have null or none or nil or some similar token/value. If the language is statically typed, maybe not all types can receive it, or maybe you need to specially mark the variables/types that can receive it. It's different, but always means the same thing - there could have been a value there, but there isn't. – Idan Arye Feb 5 at 0:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.