-1

Whenever you pass in null to JSON.parse:

console.log(JSON.parse(null))

It returns null, but I can't find any docs on why this is the case. Is there a reason why this returns null?

Other things to note:

  • This may be due to type conversion, as passing JSON.parse("null") also returns null, but is this specified anywhere?
LeoDog896
  • 3,472
  • 1
  • 15
  • 40

1 Answers1

4

This is due to JSON.parse(text\[,receiver\]) specifications, where:

  • Let jsonString be ? ToString(text).

It is converting the text to a string (null -> "null"). This is then passed to the internal JSON parser

Once it is in the parser, JSON.parse not only works with full JSON structures, but also JSON values such as number, string, and null.

Therefore, null is parsed as the null value, and returned straight back.

LeoDog896
  • 3,472
  • 1
  • 15
  • 40