JSON.stringify(NaN)为什么等于null

新手上路,请多包涵

JSON.stringify(NaN)
"null"
JSON.stringify([NaN])
"[null]"
JSON.stringify(undefined)
undefined
JSON.stringify(null)
"null"
JSON.stringify([undefined])
"[null]"

求解释原理

阅读 2.7k
1 个回答

ecma-262 规范文档 的角度解释标题的问题就非常简单。

直接看为什么 JSON.stringify(NaN) => "null"

NOTE 4
Finite numbers are stringified as if by calling ToString(number). NaN and Infinity regardless of sign are represented as the String null.

NaN 会被 当成字符串 null

为什么
JSON.stringify(undefined) => undefined
JSON.stringify([undefined]) => "[null]"

NOTE 5
Values that do not have a JSON representation (such as undefined and functions) do not produce a String. Instead they produce the undefined value. In arrays these values are represented as the String null

undefined 直接返回 undefined ; 在数组中,就被当成 字符串null;

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题