What do JSON.stringify and JSON.parse do, and what are their limits?
You decide what should happen to those values, using the optional arguments that the two functions give you.
const user = {
name: 'Ada',
created: new Date('2026-01-01T00:00:00Z'),
tags: ['a', 'b'],
missing: null,
fn: function () { return 'ignored'; },
};
const json = JSON.stringify(user);
console.log(json);
// {"name":"Ada","created":"2026-01-01T00:00:00.000Z","tags":["a","b"],"missing":null}
const parsed = JSON.parse(json);
console.log(parsed.name); // 'Ada'
console.log(parsed.created instanceof Date); // false, because it is a string now
console.log(parsed.missing); // null
// The replacer controls what is included.
console.log(JSON.stringify(user, ['name', 'tags']));
// {"name":"Ada","tags":["a","b"]}
// The third argument controls indentation.
console.log(JSON.stringify({ a: 1 }, null, 2));
There are four things to notice in that output. The function property disappeared without any error, because a function has no JSON representation at all. The date became a string, and the round trip does not bring it back as a date, so you need a reviver to rebuild it. The null value survived, and that is not true of every value in the language. And a very big integer value makes the whole call throw, because there is no lossless way to write it as a JSON number.
The replacer argument has two forms. An array of key names works as a list of things to keep, so only those properties are written. A function is called for every key and every value, and it returns what should be written instead. That is where you convert dates, drop sensitive fields, or handle big integers.
JSON.parse takes a reviver as its second argument, and the reviver is called for each key while the structure is rebuilt. That is where you turn the date strings back into date objects, and where you can check the data that arrives. A reviver that throws rejects the whole payload, which makes it a reasonable validation hook, although a proper schema validator is better for real validation work.