390 words
2 minutes

JSON.stringify Turns NaN and Infinity Into null: Keep Them Distinct

2026-11-18
Frontend
JavaScript
/
Node.js
/
Troubleshooting
Contents

If a JavaScript reading is NaN, Infinity, or -Infinity, JSON.stringify() writes it as null. A genuinely missing reading stored as null produces the same JSON. Once all four values arrive as null, the receiver has no way to tell them apart. If you control both ends of the payload, encode the three non-finite values before serialization and decode them only in the readings field.

Check what the receiver actually gets#

JSON.stringify([NaN, Infinity, -Infinity, null, 0, 42.5]) returns [null,null,null,null,0,42.5]. The number grammar in RFC 8259 excludes bare NaN and infinity tokens. ECMAScript defines the conversion to null in SerializeJSONProperty. This differs from serializing an object property whose value is undefined, where the property can disappear entirely.

Encode and decode only the readings field#

In this payload, a reading is a JavaScript number or null. On the wire, reserve three strings in readings for the non-finite values. Keep finite numbers and null as their usual JSON types. The note field can still contain literal text such as "NaN" because decoding applies only to the readings.

Save this as readings.mjs and run node readings.mjs. The example was verified with Node.js 24.13.0 on macOS.

import assert from 'node:assert/strict'
const readings = [NaN, Infinity, -Infinity, null, 0, 42.5]
const plain = JSON.stringify(readings)
assert.equal(plain, '[null,null,null,null,0,42.5]')
function encodeReading(value) {
if (value === null || Number.isFinite(value)) return value
if (Number.isNaN(value)) return 'NaN'
if (value === Infinity) return '+Infinity'
if (value === -Infinity) return '-Infinity'
throw new TypeError('Invalid reading')
}
function decodeReading(value) {
if (value === null || Number.isFinite(value)) return value
if (value === 'NaN') return NaN
if (value === '+Infinity') return Infinity
if (value === '-Infinity') return -Infinity
throw new TypeError('Invalid reading')
}
const payload = JSON.stringify({
readings: readings.map(encodeReading),
note: 'NaN',
})
assert.equal(payload, '{"readings":["NaN","+Infinity","-Infinity",null,0,42.5],"note":"NaN"}')
const parsed = JSON.parse(payload)
assert.deepEqual(parsed.readings.map(decodeReading), readings)
assert.equal(parsed.note, 'NaN')
assert.throws(() => decodeReading('42.5'), /Invalid reading/)
assert.throws(() => encodeReading('NaN'), /Invalid reading/)
console.log(plain)
console.log(payload)

The first printed line shows the four values collapsing to null. The second shows the agreed wire format. The assertions check that decoding restores all six readings, leaves the note as text, and rejects strings outside the readings contract.

Both producer and consumer need to agree on this field format. If an existing consumer requires a number or null, use a separate status field when you can extend the schema. When the schema is fixed, validate the readings before serialization and handle invalid values at the source.

References:

JSON.stringify Turns NaN and Infinity Into null: Keep Them Distinct
https://laplusda.com/en/posts/json-stringify-nan-infinity-null/
Author
Zero
Published at
2026-11-18
License
CC BY-NC-SA 4.0