Cara menggunakan encode string javascript

Masih di javascript tutorial, kali ini rumah code akan membahas tentang bagaimana caranya untuk menconvert string ke integer dengan menggunakan javascript atau biasa kita sebut convert string to int javascript.

kentapa saya tutorial tentang convert string to integer javascript? karena saya menemukan sebuah kasus yang mengambil variabel dari value dari sebuah Html input  element dan ingin menjumlahkan nya namun yang saya dapatkan bukan penjumlahan tapi malah menggabungkan nilai dari kedua input element tersebut.





HTML Input Element







Contoh script di atas merupakan kasus yang saya temukan misalkan nilai dari Input elemet a adalah 1 dan nilai dari input element b adalah 2 hasil yang saya dapatkan keitak menambakan dengan menggunakan perintah var c = a + b; akan menjadi 12. Di situ saya menyadari bahwa nilai yang saya dapatkan dari input elemet merupakan sebuah string. Untuk melakukan operasi penjumlahan berarti kita harus mengkonversi string menjadi sebuah integer.

String to int pada javascript / String to integer javascript

Bagaimana cara mengkonversi string menjadi integer pada java script? Caranya sebenar nya tidak lah sulit karena javascript sudah menyediakan fungsi untuk mengkonversi nya yaiut dengan menggunakan fungsi parseInt.

parseInt(value);

dengan menggunakan perintah di atas kita dapat melakukan konversi string ke integer javascript dengan mudah. Sekarang saya akan menggunakan fungsi parseInt pada contoh source kode





HTML Input Element







dengan merubah

 var c = a + b;

menjadi

 var c = parseInt(a) + parseInt(b);

kita akan mendapatkan penjumlahan fungsi aritmatika jadi ketika kita melakukan penjumlahan 1 + 2 kita akan mendapatkan hasil 3 bukan 12.

Pada tutorial kali ini kita akan mencoba konversi string menjadi array menggunakan methode split(); Split() digunakan untuk membagi string menjadi array substring dan mengembalikan array baru tanpa mengubah string asli

The

JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(1906, 0, 2, 15, 4, 5));
// '"1906-01-02T15:04:05.000Z"'

JSON.stringify({ x: 5, y: 6 });
// '{"x":5,"y":6}'
JSON.stringify([new Number(3), new String("false"), new Boolean(false)]);
// '[3,"false",false]'

// String-keyed array elements are not enumerable and make no sense in JSON
const a = ["foo", "bar"];
a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
JSON.stringify(a);
// '["foo","bar"]'

JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] });
// '{"x":[10,null,null,null]}'

// Standard data structures
JSON.stringify([
  new Set([1]),
  new Map([[1, 2]]),
  new WeakSet([{ a: 1 }]),
  new WeakMap([[{ a: 1 }, 2]]),
]);
// '[{},{},{},{}]'

// TypedArray
JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
// '[{"0":1},{"0":1},{"0":1}]'
JSON.stringify([
  new Uint8Array([1]),
  new Uint8ClampedArray([1]),
  new Uint16Array([1]),
  new Uint32Array([1]),
]);
// '[{"0":1},{"0":1},{"0":1},{"0":1}]'
JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
// '[{"0":1},{"0":1}]'

// toJSON()
JSON.stringify({
  x: 5,
  y: 6,
  toJSON() {
    return this.x + this.y;
  },
});
// '11'

// Symbols:
JSON.stringify({ x: undefined, y: Object, z: Symbol("") });
// '{}'
JSON.stringify({ [Symbol("foo")]: "foo" });
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, [Symbol.for("foo")]);
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

// Non-enumerable properties:
JSON.stringify(
  Object.create(null, {
    x: { value: "x", enumerable: false },
    y: { value: "y", enumerable: true },
  }),
);
// '{"y":"y"}'

// BigInt values throw
JSON.stringify({ x: 2n });
// TypeError: BigInt value can't be serialized in JSON
2 static method converts a JavaScript value to a JSON string, optionally replacing values if a replacer function is specified or optionally including only the specified properties if a replacer array is specified.

JSON.stringify(value)
JSON.stringify(value, replacer)
JSON.stringify(value, replacer, space)

JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(1906, 0, 2, 15, 4, 5));
// '"1906-01-02T15:04:05.000Z"'

JSON.stringify({ x: 5, y: 6 });
// '{"x":5,"y":6}'
JSON.stringify([new Number(3), new String("false"), new Boolean(false)]);
// '[3,"false",false]'

// String-keyed array elements are not enumerable and make no sense in JSON
const a = ["foo", "bar"];
a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
JSON.stringify(a);
// '["foo","bar"]'

JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] });
// '{"x":[10,null,null,null]}'

// Standard data structures
JSON.stringify([
  new Set([1]),
  new Map([[1, 2]]),
  new WeakSet([{ a: 1 }]),
  new WeakMap([[{ a: 1 }, 2]]),
]);
// '[{},{},{},{}]'

// TypedArray
JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
// '[{"0":1},{"0":1},{"0":1}]'
JSON.stringify([
  new Uint8Array([1]),
  new Uint8ClampedArray([1]),
  new Uint16Array([1]),
  new Uint32Array([1]),
]);
// '[{"0":1},{"0":1},{"0":1},{"0":1}]'
JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
// '[{"0":1},{"0":1}]'

// toJSON()
JSON.stringify({
  x: 5,
  y: 6,
  toJSON() {
    return this.x + this.y;
  },
});
// '11'

// Symbols:
JSON.stringify({ x: undefined, y: Object, z: Symbol("") });
// '{}'
JSON.stringify({ [Symbol("foo")]: "foo" });
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, [Symbol.for("foo")]);
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

// Non-enumerable properties:
JSON.stringify(
  Object.create(null, {
    x: { value: "x", enumerable: false },
    y: { value: "y", enumerable: true },
  }),
);
// '{"y":"y"}'

// BigInt values throw
JSON.stringify({ x: 2n });
// TypeError: BigInt value can't be serialized in JSON
3

The value to convert to a JSON string.

JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(1906, 0, 2, 15, 4, 5));
// '"1906-01-02T15:04:05.000Z"'

JSON.stringify({ x: 5, y: 6 });
// '{"x":5,"y":6}'
JSON.stringify([new Number(3), new String("false"), new Boolean(false)]);
// '[3,"false",false]'

// String-keyed array elements are not enumerable and make no sense in JSON
const a = ["foo", "bar"];
a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
JSON.stringify(a);
// '["foo","bar"]'

JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] });
// '{"x":[10,null,null,null]}'

// Standard data structures
JSON.stringify([
  new Set([1]),
  new Map([[1, 2]]),
  new WeakSet([{ a: 1 }]),
  new WeakMap([[{ a: 1 }, 2]]),
]);
// '[{},{},{},{}]'

// TypedArray
JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
// '[{"0":1},{"0":1},{"0":1}]'
JSON.stringify([
  new Uint8Array([1]),
  new Uint8ClampedArray([1]),
  new Uint16Array([1]),
  new Uint32Array([1]),
]);
// '[{"0":1},{"0":1},{"0":1},{"0":1}]'
JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
// '[{"0":1},{"0":1}]'

// toJSON()
JSON.stringify({
  x: 5,
  y: 6,
  toJSON() {
    return this.x + this.y;
  },
});
// '11'

// Symbols:
JSON.stringify({ x: undefined, y: Object, z: Symbol("") });
// '{}'
JSON.stringify({ [Symbol("foo")]: "foo" });
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, [Symbol.for("foo")]);
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

// Non-enumerable properties:
JSON.stringify(
  Object.create(null, {
    x: { value: "x", enumerable: false },
    y: { value: "y", enumerable: true },
  }),
);
// '{"y":"y"}'

// BigInt values throw
JSON.stringify({ x: 2n });
// TypeError: BigInt value can't be serialized in JSON
4 Optional

A function that alters the behavior of the stringification process, or an array of strings and numbers that specifies properties of

JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(1906, 0, 2, 15, 4, 5));
// '"1906-01-02T15:04:05.000Z"'

JSON.stringify({ x: 5, y: 6 });
// '{"x":5,"y":6}'
JSON.stringify([new Number(3), new String("false"), new Boolean(false)]);
// '[3,"false",false]'

// String-keyed array elements are not enumerable and make no sense in JSON
const a = ["foo", "bar"];
a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
JSON.stringify(a);
// '["foo","bar"]'

JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] });
// '{"x":[10,null,null,null]}'

// Standard data structures
JSON.stringify([
  new Set([1]),
  new Map([[1, 2]]),
  new WeakSet([{ a: 1 }]),
  new WeakMap([[{ a: 1 }, 2]]),
]);
// '[{},{},{},{}]'

// TypedArray
JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
// '[{"0":1},{"0":1},{"0":1}]'
JSON.stringify([
  new Uint8Array([1]),
  new Uint8ClampedArray([1]),
  new Uint16Array([1]),
  new Uint32Array([1]),
]);
// '[{"0":1},{"0":1},{"0":1},{"0":1}]'
JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
// '[{"0":1},{"0":1}]'

// toJSON()
JSON.stringify({
  x: 5,
  y: 6,
  toJSON() {
    return this.x + this.y;
  },
});
// '11'

// Symbols:
JSON.stringify({ x: undefined, y: Object, z: Symbol("") });
// '{}'
JSON.stringify({ [Symbol("foo")]: "foo" });
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, [Symbol.for("foo")]);
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

// Non-enumerable properties:
JSON.stringify(
  Object.create(null, {
    x: { value: "x", enumerable: false },
    y: { value: "y", enumerable: true },
  }),
);
// '{"y":"y"}'

// BigInt values throw
JSON.stringify({ x: 2n });
// TypeError: BigInt value can't be serialized in JSON
3 to be included in the output. If
JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(1906, 0, 2, 15, 4, 5));
// '"1906-01-02T15:04:05.000Z"'

JSON.stringify({ x: 5, y: 6 });
// '{"x":5,"y":6}'
JSON.stringify([new Number(3), new String("false"), new Boolean(false)]);
// '[3,"false",false]'

// String-keyed array elements are not enumerable and make no sense in JSON
const a = ["foo", "bar"];
a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
JSON.stringify(a);
// '["foo","bar"]'

JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] });
// '{"x":[10,null,null,null]}'

// Standard data structures
JSON.stringify([
  new Set([1]),
  new Map([[1, 2]]),
  new WeakSet([{ a: 1 }]),
  new WeakMap([[{ a: 1 }, 2]]),
]);
// '[{},{},{},{}]'

// TypedArray
JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
// '[{"0":1},{"0":1},{"0":1}]'
JSON.stringify([
  new Uint8Array([1]),
  new Uint8ClampedArray([1]),
  new Uint16Array([1]),
  new Uint32Array([1]),
]);
// '[{"0":1},{"0":1},{"0":1},{"0":1}]'
JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
// '[{"0":1},{"0":1}]'

// toJSON()
JSON.stringify({
  x: 5,
  y: 6,
  toJSON() {
    return this.x + this.y;
  },
});
// '11'

// Symbols:
JSON.stringify({ x: undefined, y: Object, z: Symbol("") });
// '{}'
JSON.stringify({ [Symbol("foo")]: "foo" });
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, [Symbol.for("foo")]);
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

// Non-enumerable properties:
JSON.stringify(
  Object.create(null, {
    x: { value: "x", enumerable: false },
    y: { value: "y", enumerable: true },
  }),
);
// '{"y":"y"}'

// BigInt values throw
JSON.stringify({ x: 2n });
// TypeError: BigInt value can't be serialized in JSON
4 is an array, all elements in this array that are not strings or numbers (either primitives or wrapper objects), including
JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(1906, 0, 2, 15, 4, 5));
// '"1906-01-02T15:04:05.000Z"'

JSON.stringify({ x: 5, y: 6 });
// '{"x":5,"y":6}'
JSON.stringify([new Number(3), new String("false"), new Boolean(false)]);
// '[3,"false",false]'

// String-keyed array elements are not enumerable and make no sense in JSON
const a = ["foo", "bar"];
a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
JSON.stringify(a);
// '["foo","bar"]'

JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] });
// '{"x":[10,null,null,null]}'

// Standard data structures
JSON.stringify([
  new Set([1]),
  new Map([[1, 2]]),
  new WeakSet([{ a: 1 }]),
  new WeakMap([[{ a: 1 }, 2]]),
]);
// '[{},{},{},{}]'

// TypedArray
JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
// '[{"0":1},{"0":1},{"0":1}]'
JSON.stringify([
  new Uint8Array([1]),
  new Uint8ClampedArray([1]),
  new Uint16Array([1]),
  new Uint32Array([1]),
]);
// '[{"0":1},{"0":1},{"0":1},{"0":1}]'
JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
// '[{"0":1},{"0":1}]'

// toJSON()
JSON.stringify({
  x: 5,
  y: 6,
  toJSON() {
    return this.x + this.y;
  },
});
// '11'

// Symbols:
JSON.stringify({ x: undefined, y: Object, z: Symbol("") });
// '{}'
JSON.stringify({ [Symbol("foo")]: "foo" });
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, [Symbol.for("foo")]);
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

// Non-enumerable properties:
JSON.stringify(
  Object.create(null, {
    x: { value: "x", enumerable: false },
    y: { value: "y", enumerable: true },
  }),
);
// '{"y":"y"}'

// BigInt values throw
JSON.stringify({ x: 2n });
// TypeError: BigInt value can't be serialized in JSON
7 values, are completely ignored. If
JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(1906, 0, 2, 15, 4, 5));
// '"1906-01-02T15:04:05.000Z"'

JSON.stringify({ x: 5, y: 6 });
// '{"x":5,"y":6}'
JSON.stringify([new Number(3), new String("false"), new Boolean(false)]);
// '[3,"false",false]'

// String-keyed array elements are not enumerable and make no sense in JSON
const a = ["foo", "bar"];
a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
JSON.stringify(a);
// '["foo","bar"]'

JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] });
// '{"x":[10,null,null,null]}'

// Standard data structures
JSON.stringify([
  new Set([1]),
  new Map([[1, 2]]),
  new WeakSet([{ a: 1 }]),
  new WeakMap([[{ a: 1 }, 2]]),
]);
// '[{},{},{},{}]'

// TypedArray
JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
// '[{"0":1},{"0":1},{"0":1}]'
JSON.stringify([
  new Uint8Array([1]),
  new Uint8ClampedArray([1]),
  new Uint16Array([1]),
  new Uint32Array([1]),
]);
// '[{"0":1},{"0":1},{"0":1},{"0":1}]'
JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
// '[{"0":1},{"0":1}]'

// toJSON()
JSON.stringify({
  x: 5,
  y: 6,
  toJSON() {
    return this.x + this.y;
  },
});
// '11'

// Symbols:
JSON.stringify({ x: undefined, y: Object, z: Symbol("") });
// '{}'
JSON.stringify({ [Symbol("foo")]: "foo" });
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, [Symbol.for("foo")]);
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

// Non-enumerable properties:
JSON.stringify(
  Object.create(null, {
    x: { value: "x", enumerable: false },
    y: { value: "y", enumerable: true },
  }),
);
// '{"y":"y"}'

// BigInt values throw
JSON.stringify({ x: 2n });
// TypeError: BigInt value can't be serialized in JSON
4 is anything other than a function or an array (e.g.
JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(1906, 0, 2, 15, 4, 5));
// '"1906-01-02T15:04:05.000Z"'

JSON.stringify({ x: 5, y: 6 });
// '{"x":5,"y":6}'
JSON.stringify([new Number(3), new String("false"), new Boolean(false)]);
// '[3,"false",false]'

// String-keyed array elements are not enumerable and make no sense in JSON
const a = ["foo", "bar"];
a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
JSON.stringify(a);
// '["foo","bar"]'

JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] });
// '{"x":[10,null,null,null]}'

// Standard data structures
JSON.stringify([
  new Set([1]),
  new Map([[1, 2]]),
  new WeakSet([{ a: 1 }]),
  new WeakMap([[{ a: 1 }, 2]]),
]);
// '[{},{},{},{}]'

// TypedArray
JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
// '[{"0":1},{"0":1},{"0":1}]'
JSON.stringify([
  new Uint8Array([1]),
  new Uint8ClampedArray([1]),
  new Uint16Array([1]),
  new Uint32Array([1]),
]);
// '[{"0":1},{"0":1},{"0":1},{"0":1}]'
JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
// '[{"0":1},{"0":1}]'

// toJSON()
JSON.stringify({
  x: 5,
  y: 6,
  toJSON() {
    return this.x + this.y;
  },
});
// '11'

// Symbols:
JSON.stringify({ x: undefined, y: Object, z: Symbol("") });
// '{}'
JSON.stringify({ [Symbol("foo")]: "foo" });
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, [Symbol.for("foo")]);
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

// Non-enumerable properties:
JSON.stringify(
  Object.create(null, {
    x: { value: "x", enumerable: false },
    y: { value: "y", enumerable: true },
  }),
);
// '{"y":"y"}'

// BigInt values throw
JSON.stringify({ x: 2n });
// TypeError: BigInt value can't be serialized in JSON
9 or not provided), all string-keyed properties of the object are included in the resulting JSON string.

function replacer(key, value) {
  // Filtering out properties
  if (typeof value === "string") {
    return undefined;
  }
  return value;
}

const foo = {
  foundation: "Mozilla",
  model: "box",
  week: 45,
  transport: "car",
  month: 7,
};
JSON.stringify(foo, replacer);
// '{"week":45,"month":7}'
0 Optional

A string or number that's used to insert white space (including indentation, line break characters, etc.) into the output JSON string for readability purposes.

If this is a number, it indicates the number of space characters to be used as indentation, clamped to 10 (that is, any number greater than

function replacer(key, value) {
  // Filtering out properties
  if (typeof value === "string") {
    return undefined;
  }
  return value;
}

const foo = {
  foundation: "Mozilla",
  model: "box",
  week: 45,
  transport: "car",
  month: 7,
};
JSON.stringify(foo, replacer);
// '{"week":45,"month":7}'
1 is treated as if it were
function replacer(key, value) {
  // Filtering out properties
  if (typeof value === "string") {
    return undefined;
  }
  return value;
}

const foo = {
  foundation: "Mozilla",
  model: "box",
  week: 45,
  transport: "car",
  month: 7,
};
JSON.stringify(foo, replacer);
// '{"week":45,"month":7}'
1). Values less than 1 indicate that no space should be used.

If this is a string, the string (or the first 10 characters of the string, if it's longer than that) is inserted before every nested object or array.

If

function replacer(key, value) {
  // Filtering out properties
  if (typeof value === "string") {
    return undefined;
  }
  return value;
}

const foo = {
  foundation: "Mozilla",
  model: "box",
  week: 45,
  transport: "car",
  month: 7,
};
JSON.stringify(foo, replacer);
// '{"week":45,"month":7}'
0 is anything other than a string or number (can be either a primitive or a wrapper object) — for example, is
JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(1906, 0, 2, 15, 4, 5));
// '"1906-01-02T15:04:05.000Z"'

JSON.stringify({ x: 5, y: 6 });
// '{"x":5,"y":6}'
JSON.stringify([new Number(3), new String("false"), new Boolean(false)]);
// '[3,"false",false]'

// String-keyed array elements are not enumerable and make no sense in JSON
const a = ["foo", "bar"];
a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
JSON.stringify(a);
// '["foo","bar"]'

JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] });
// '{"x":[10,null,null,null]}'

// Standard data structures
JSON.stringify([
  new Set([1]),
  new Map([[1, 2]]),
  new WeakSet([{ a: 1 }]),
  new WeakMap([[{ a: 1 }, 2]]),
]);
// '[{},{},{},{}]'

// TypedArray
JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
// '[{"0":1},{"0":1},{"0":1}]'
JSON.stringify([
  new Uint8Array([1]),
  new Uint8ClampedArray([1]),
  new Uint16Array([1]),
  new Uint32Array([1]),
]);
// '[{"0":1},{"0":1},{"0":1},{"0":1}]'
JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
// '[{"0":1},{"0":1}]'

// toJSON()
JSON.stringify({
  x: 5,
  y: 6,
  toJSON() {
    return this.x + this.y;
  },
});
// '11'

// Symbols:
JSON.stringify({ x: undefined, y: Object, z: Symbol("") });
// '{}'
JSON.stringify({ [Symbol("foo")]: "foo" });
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, [Symbol.for("foo")]);
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

// Non-enumerable properties:
JSON.stringify(
  Object.create(null, {
    x: { value: "x", enumerable: false },
    y: { value: "y", enumerable: true },
  }),
);
// '{"y":"y"}'

// BigInt values throw
JSON.stringify({ x: 2n });
// TypeError: BigInt value can't be serialized in JSON
9 or not provided — no white space is used.

A JSON string representing the given value, or undefined.

function replacer(key, value) {
  // Filtering out properties
  if (typeof value === "string") {
    return undefined;
  }
  return value;
}

const foo = {
  foundation: "Mozilla",
  model: "box",
  week: 45,
  transport: "car",
  month: 7,
};
JSON.stringify(foo, replacer);
// '{"week":45,"month":7}'
5

Thrown if one of the following is true:

  • JSON.stringify({}); // '{}'
    JSON.stringify(true); // 'true'
    JSON.stringify("foo"); // '"foo"'
    JSON.stringify([1, "false", false]); // '[1,"false",false]'
    JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
    JSON.stringify({ x: 5 }); // '{"x":5}'
    
    JSON.stringify(new Date(1906, 0, 2, 15, 4, 5));
    // '"1906-01-02T15:04:05.000Z"'
    
    JSON.stringify({ x: 5, y: 6 });
    // '{"x":5,"y":6}'
    JSON.stringify([new Number(3), new String("false"), new Boolean(false)]);
    // '[3,"false",false]'
    
    // String-keyed array elements are not enumerable and make no sense in JSON
    const a = ["foo", "bar"];
    a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
    JSON.stringify(a);
    // '["foo","bar"]'
    
    JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] });
    // '{"x":[10,null,null,null]}'
    
    // Standard data structures
    JSON.stringify([
      new Set([1]),
      new Map([[1, 2]]),
      new WeakSet([{ a: 1 }]),
      new WeakMap([[{ a: 1 }, 2]]),
    ]);
    // '[{},{},{},{}]'
    
    // TypedArray
    JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
    // '[{"0":1},{"0":1},{"0":1}]'
    JSON.stringify([
      new Uint8Array([1]),
      new Uint8ClampedArray([1]),
      new Uint16Array([1]),
      new Uint32Array([1]),
    ]);
    // '[{"0":1},{"0":1},{"0":1},{"0":1}]'
    JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
    // '[{"0":1},{"0":1}]'
    
    // toJSON()
    JSON.stringify({
      x: 5,
      y: 6,
      toJSON() {
        return this.x + this.y;
      },
    });
    // '11'
    
    // Symbols:
    JSON.stringify({ x: undefined, y: Object, z: Symbol("") });
    // '{}'
    JSON.stringify({ [Symbol("foo")]: "foo" });
    // '{}'
    JSON.stringify({ [Symbol.for("foo")]: "foo" }, [Symbol.for("foo")]);
    // '{}'
    JSON.stringify({ [Symbol.for("foo")]: "foo" }, (k, v) => {
      if (typeof k === "symbol") {
        return "a symbol";
      }
    });
    // undefined
    
    // Non-enumerable properties:
    JSON.stringify(
      Object.create(null, {
        x: { value: "x", enumerable: false },
        y: { value: "y", enumerable: true },
      }),
    );
    // '{"y":"y"}'
    
    // BigInt values throw
    JSON.stringify({ x: 2n });
    // TypeError: BigInt value can't be serialized in JSON
    
    3 contains a circular reference.
  • A
    function replacer(key, value) {
      // Filtering out properties
      if (typeof value === "string") {
        return undefined;
      }
      return value;
    }
    
    const foo = {
      foundation: "Mozilla",
      model: "box",
      week: 45,
      transport: "car",
      month: 7,
    };
    JSON.stringify(foo, replacer);
    // '{"week":45,"month":7}'
    
    7 value is encountered.

JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(1906, 0, 2, 15, 4, 5));
// '"1906-01-02T15:04:05.000Z"'

JSON.stringify({ x: 5, y: 6 });
// '{"x":5,"y":6}'
JSON.stringify([new Number(3), new String("false"), new Boolean(false)]);
// '[3,"false",false]'

// String-keyed array elements are not enumerable and make no sense in JSON
const a = ["foo", "bar"];
a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
JSON.stringify(a);
// '["foo","bar"]'

JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] });
// '{"x":[10,null,null,null]}'

// Standard data structures
JSON.stringify([
  new Set([1]),
  new Map([[1, 2]]),
  new WeakSet([{ a: 1 }]),
  new WeakMap([[{ a: 1 }, 2]]),
]);
// '[{},{},{},{}]'

// TypedArray
JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
// '[{"0":1},{"0":1},{"0":1}]'
JSON.stringify([
  new Uint8Array([1]),
  new Uint8ClampedArray([1]),
  new Uint16Array([1]),
  new Uint32Array([1]),
]);
// '[{"0":1},{"0":1},{"0":1},{"0":1}]'
JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
// '[{"0":1},{"0":1}]'

// toJSON()
JSON.stringify({
  x: 5,
  y: 6,
  toJSON() {
    return this.x + this.y;
  },
});
// '11'

// Symbols:
JSON.stringify({ x: undefined, y: Object, z: Symbol("") });
// '{}'
JSON.stringify({ [Symbol("foo")]: "foo" });
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, [Symbol.for("foo")]);
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

// Non-enumerable properties:
JSON.stringify(
  Object.create(null, {
    x: { value: "x", enumerable: false },
    y: { value: "y", enumerable: true },
  }),
);
// '{"y":"y"}'

// BigInt values throw
JSON.stringify({ x: 2n });
// TypeError: BigInt value can't be serialized in JSON
2 converts a value to the JSON notation that the value represents. Values are stringified in the following manner:

  • function replacer(key, value) {
      // Filtering out properties
      if (typeof value === "string") {
        return undefined;
      }
      return value;
    }
    
    const foo = {
      foundation: "Mozilla",
      model: "box",
      week: 45,
      transport: "car",
      month: 7,
    };
    JSON.stringify(foo, replacer);
    // '{"week":45,"month":7}'
    
    9,
    function makeReplacer() {
      let isInitial = true;
    
      return (key, value) => {
        if (isInitial) {
          isInitial = false;
          return value;
        }
        if (key === "") {
          // Omit all properties with name "" (except the initial object)
          return undefined;
        }
        return value;
      };
    }
    
    const replacer = makeReplacer();
    console.log(JSON.stringify({ "": 1, b: 2 }, replacer)); // "{"b":2}"
    
    0,
    function makeReplacer() {
      let isInitial = true;
    
      return (key, value) => {
        if (isInitial) {
          isInitial = false;
          return value;
        }
        if (key === "") {
          // Omit all properties with name "" (except the initial object)
          return undefined;
        }
        return value;
      };
    }
    
    const replacer = makeReplacer();
    console.log(JSON.stringify({ "": 1, b: 2 }, replacer)); // "{"b":2}"
    
    1, and
    function replacer(key, value) {
      // Filtering out properties
      if (typeof value === "string") {
        return undefined;
      }
      return value;
    }
    
    const foo = {
      foundation: "Mozilla",
      model: "box",
      week: 45,
      transport: "car",
      month: 7,
    };
    JSON.stringify(foo, replacer);
    // '{"week":45,"month":7}'
    
    7 (obtainable via
    function makeReplacer() {
      let isInitial = true;
    
      return (key, value) => {
        if (isInitial) {
          isInitial = false;
          return value;
        }
        if (key === "") {
          // Omit all properties with name "" (except the initial object)
          return undefined;
        }
        return value;
      };
    }
    
    const replacer = makeReplacer();
    console.log(JSON.stringify({ "": 1, b: 2 }, replacer)); // "{"b":2}"
    
    3) objects are converted to the corresponding primitive values during stringification, in accordance with the traditional conversion semantics.
    JSON.stringify({}); // '{}'
    JSON.stringify(true); // 'true'
    JSON.stringify("foo"); // '"foo"'
    JSON.stringify([1, "false", false]); // '[1,"false",false]'
    JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
    JSON.stringify({ x: 5 }); // '{"x":5}'
    
    JSON.stringify(new Date(1906, 0, 2, 15, 4, 5));
    // '"1906-01-02T15:04:05.000Z"'
    
    JSON.stringify({ x: 5, y: 6 });
    // '{"x":5,"y":6}'
    JSON.stringify([new Number(3), new String("false"), new Boolean(false)]);
    // '[3,"false",false]'
    
    // String-keyed array elements are not enumerable and make no sense in JSON
    const a = ["foo", "bar"];
    a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
    JSON.stringify(a);
    // '["foo","bar"]'
    
    JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] });
    // '{"x":[10,null,null,null]}'
    
    // Standard data structures
    JSON.stringify([
      new Set([1]),
      new Map([[1, 2]]),
      new WeakSet([{ a: 1 }]),
      new WeakMap([[{ a: 1 }, 2]]),
    ]);
    // '[{},{},{},{}]'
    
    // TypedArray
    JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
    // '[{"0":1},{"0":1},{"0":1}]'
    JSON.stringify([
      new Uint8Array([1]),
      new Uint8ClampedArray([1]),
      new Uint16Array([1]),
      new Uint32Array([1]),
    ]);
    // '[{"0":1},{"0":1},{"0":1},{"0":1}]'
    JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
    // '[{"0":1},{"0":1}]'
    
    // toJSON()
    JSON.stringify({
      x: 5,
      y: 6,
      toJSON() {
        return this.x + this.y;
      },
    });
    // '11'
    
    // Symbols:
    JSON.stringify({ x: undefined, y: Object, z: Symbol("") });
    // '{}'
    JSON.stringify({ [Symbol("foo")]: "foo" });
    // '{}'
    JSON.stringify({ [Symbol.for("foo")]: "foo" }, [Symbol.for("foo")]);
    // '{}'
    JSON.stringify({ [Symbol.for("foo")]: "foo" }, (k, v) => {
      if (typeof k === "symbol") {
        return "a symbol";
      }
    });
    // undefined
    
    // Non-enumerable properties:
    JSON.stringify(
      Object.create(null, {
        x: { value: "x", enumerable: false },
        y: { value: "y", enumerable: true },
      }),
    );
    // '{"y":"y"}'
    
    // BigInt values throw
    JSON.stringify({ x: 2n });
    // TypeError: BigInt value can't be serialized in JSON
    
    7 objects (obtainable via
    function makeReplacer() {
      let isInitial = true;
    
      return (key, value) => {
        if (isInitial) {
          isInitial = false;
          return value;
        }
        if (key === "") {
          // Omit all properties with name "" (except the initial object)
          return undefined;
        }
        return value;
      };
    }
    
    const replacer = makeReplacer();
    console.log(JSON.stringify({ "": 1, b: 2 }, replacer)); // "{"b":2}"
    
    3) are treated as plain objects.
  • Attempting to serialize
    function replacer(key, value) {
      // Filtering out properties
      if (typeof value === "string") {
        return undefined;
      }
      return value;
    }
    
    const foo = {
      foundation: "Mozilla",
      model: "box",
      week: 45,
      transport: "car",
      month: 7,
    };
    JSON.stringify(foo, replacer);
    // '{"week":45,"month":7}'
    
    7 values will throw. However, if the BigInt has a
    function makeReplacer() {
      let isInitial = true;
    
      return (key, value) => {
        if (isInitial) {
          isInitial = false;
          return value;
        }
        if (key === "") {
          // Omit all properties with name "" (except the initial object)
          return undefined;
        }
        return value;
      };
    }
    
    const replacer = makeReplacer();
    console.log(JSON.stringify({ "": 1, b: 2 }, replacer)); // "{"b":2}"
    
    7 method (through monkey patching:
    function makeReplacer() {
      let isInitial = true;
    
      return (key, value) => {
        if (isInitial) {
          isInitial = false;
          return value;
        }
        if (key === "") {
          // Omit all properties with name "" (except the initial object)
          return undefined;
        }
        return value;
      };
    }
    
    const replacer = makeReplacer();
    console.log(JSON.stringify({ "": 1, b: 2 }, replacer)); // "{"b":2}"
    
    8), that method can provide the serialization result. This constraint ensures that a proper serialization (and, very likely, its accompanying deserialization) behavior is always explicitly provided by the user.
  • function makeReplacer() {
      let isInitial = true;
    
      return (key, value) => {
        if (isInitial) {
          isInitial = false;
          return value;
        }
        if (key === "") {
          // Omit all properties with name "" (except the initial object)
          return undefined;
        }
        return value;
      };
    }
    
    const replacer = makeReplacer();
    console.log(JSON.stringify({ "": 1, b: 2 }, replacer)); // "{"b":2}"
    
    9,
    const foo = {
      foundation: "Mozilla",
      model: "box",
      week: 45,
      transport: "car",
      month: 7,
    };
    
    JSON.stringify(foo, ["week", "month"]);
    // '{"week":45,"month":7}', only keep "week" and "month" properties
    
    0, and
    JSON.stringify({}); // '{}'
    JSON.stringify(true); // 'true'
    JSON.stringify("foo"); // '"foo"'
    JSON.stringify([1, "false", false]); // '[1,"false",false]'
    JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
    JSON.stringify({ x: 5 }); // '{"x":5}'
    
    JSON.stringify(new Date(1906, 0, 2, 15, 4, 5));
    // '"1906-01-02T15:04:05.000Z"'
    
    JSON.stringify({ x: 5, y: 6 });
    // '{"x":5,"y":6}'
    JSON.stringify([new Number(3), new String("false"), new Boolean(false)]);
    // '[3,"false",false]'
    
    // String-keyed array elements are not enumerable and make no sense in JSON
    const a = ["foo", "bar"];
    a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
    JSON.stringify(a);
    // '["foo","bar"]'
    
    JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] });
    // '{"x":[10,null,null,null]}'
    
    // Standard data structures
    JSON.stringify([
      new Set([1]),
      new Map([[1, 2]]),
      new WeakSet([{ a: 1 }]),
      new WeakMap([[{ a: 1 }, 2]]),
    ]);
    // '[{},{},{},{}]'
    
    // TypedArray
    JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
    // '[{"0":1},{"0":1},{"0":1}]'
    JSON.stringify([
      new Uint8Array([1]),
      new Uint8ClampedArray([1]),
      new Uint16Array([1]),
      new Uint32Array([1]),
    ]);
    // '[{"0":1},{"0":1},{"0":1},{"0":1}]'
    JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
    // '[{"0":1},{"0":1}]'
    
    // toJSON()
    JSON.stringify({
      x: 5,
      y: 6,
      toJSON() {
        return this.x + this.y;
      },
    });
    // '11'
    
    // Symbols:
    JSON.stringify({ x: undefined, y: Object, z: Symbol("") });
    // '{}'
    JSON.stringify({ [Symbol("foo")]: "foo" });
    // '{}'
    JSON.stringify({ [Symbol.for("foo")]: "foo" }, [Symbol.for("foo")]);
    // '{}'
    JSON.stringify({ [Symbol.for("foo")]: "foo" }, (k, v) => {
      if (typeof k === "symbol") {
        return "a symbol";
      }
    });
    // undefined
    
    // Non-enumerable properties:
    JSON.stringify(
      Object.create(null, {
        x: { value: "x", enumerable: false },
        y: { value: "y", enumerable: true },
      }),
    );
    // '{"y":"y"}'
    
    // BigInt values throw
    JSON.stringify({ x: 2n });
    // TypeError: BigInt value can't be serialized in JSON
    
    7 values are not valid JSON values. If any such values are encountered during conversion, they are either omitted (when found in an object) or changed to
    JSON.stringify({}); // '{}'
    JSON.stringify(true); // 'true'
    JSON.stringify("foo"); // '"foo"'
    JSON.stringify([1, "false", false]); // '[1,"false",false]'
    JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
    JSON.stringify({ x: 5 }); // '{"x":5}'
    
    JSON.stringify(new Date(1906, 0, 2, 15, 4, 5));
    // '"1906-01-02T15:04:05.000Z"'
    
    JSON.stringify({ x: 5, y: 6 });
    // '{"x":5,"y":6}'
    JSON.stringify([new Number(3), new String("false"), new Boolean(false)]);
    // '[3,"false",false]'
    
    // String-keyed array elements are not enumerable and make no sense in JSON
    const a = ["foo", "bar"];
    a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
    JSON.stringify(a);
    // '["foo","bar"]'
    
    JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] });
    // '{"x":[10,null,null,null]}'
    
    // Standard data structures
    JSON.stringify([
      new Set([1]),
      new Map([[1, 2]]),
      new WeakSet([{ a: 1 }]),
      new WeakMap([[{ a: 1 }, 2]]),
    ]);
    // '[{},{},{},{}]'
    
    // TypedArray
    JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
    // '[{"0":1},{"0":1},{"0":1}]'
    JSON.stringify([
      new Uint8Array([1]),
      new Uint8ClampedArray([1]),
      new Uint16Array([1]),
      new Uint32Array([1]),
    ]);
    // '[{"0":1},{"0":1},{"0":1},{"0":1}]'
    JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
    // '[{"0":1},{"0":1}]'
    
    // toJSON()
    JSON.stringify({
      x: 5,
      y: 6,
      toJSON() {
        return this.x + this.y;
      },
    });
    // '11'
    
    // Symbols:
    JSON.stringify({ x: undefined, y: Object, z: Symbol("") });
    // '{}'
    JSON.stringify({ [Symbol("foo")]: "foo" });
    // '{}'
    JSON.stringify({ [Symbol.for("foo")]: "foo" }, [Symbol.for("foo")]);
    // '{}'
    JSON.stringify({ [Symbol.for("foo")]: "foo" }, (k, v) => {
      if (typeof k === "symbol") {
        return "a symbol";
      }
    });
    // undefined
    
    // Non-enumerable properties:
    JSON.stringify(
      Object.create(null, {
        x: { value: "x", enumerable: false },
        y: { value: "y", enumerable: true },
      }),
    );
    // '{"y":"y"}'
    
    // BigInt values throw
    JSON.stringify({ x: 2n });
    // TypeError: BigInt value can't be serialized in JSON
    
    9 (when found in an array).
    JSON.stringify({}); // '{}'
    JSON.stringify(true); // 'true'
    JSON.stringify("foo"); // '"foo"'
    JSON.stringify([1, "false", false]); // '[1,"false",false]'
    JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
    JSON.stringify({ x: 5 }); // '{"x":5}'
    
    JSON.stringify(new Date(1906, 0, 2, 15, 4, 5));
    // '"1906-01-02T15:04:05.000Z"'
    
    JSON.stringify({ x: 5, y: 6 });
    // '{"x":5,"y":6}'
    JSON.stringify([new Number(3), new String("false"), new Boolean(false)]);
    // '[3,"false",false]'
    
    // String-keyed array elements are not enumerable and make no sense in JSON
    const a = ["foo", "bar"];
    a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
    JSON.stringify(a);
    // '["foo","bar"]'
    
    JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] });
    // '{"x":[10,null,null,null]}'
    
    // Standard data structures
    JSON.stringify([
      new Set([1]),
      new Map([[1, 2]]),
      new WeakSet([{ a: 1 }]),
      new WeakMap([[{ a: 1 }, 2]]),
    ]);
    // '[{},{},{},{}]'
    
    // TypedArray
    JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
    // '[{"0":1},{"0":1},{"0":1}]'
    JSON.stringify([
      new Uint8Array([1]),
      new Uint8ClampedArray([1]),
      new Uint16Array([1]),
      new Uint32Array([1]),
    ]);
    // '[{"0":1},{"0":1},{"0":1},{"0":1}]'
    JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
    // '[{"0":1},{"0":1}]'
    
    // toJSON()
    JSON.stringify({
      x: 5,
      y: 6,
      toJSON() {
        return this.x + this.y;
      },
    });
    // '11'
    
    // Symbols:
    JSON.stringify({ x: undefined, y: Object, z: Symbol("") });
    // '{}'
    JSON.stringify({ [Symbol("foo")]: "foo" });
    // '{}'
    JSON.stringify({ [Symbol.for("foo")]: "foo" }, [Symbol.for("foo")]);
    // '{}'
    JSON.stringify({ [Symbol.for("foo")]: "foo" }, (k, v) => {
      if (typeof k === "symbol") {
        return "a symbol";
      }
    });
    // undefined
    
    // Non-enumerable properties:
    JSON.stringify(
      Object.create(null, {
        x: { value: "x", enumerable: false },
        y: { value: "y", enumerable: true },
      }),
    );
    // '{"y":"y"}'
    
    // BigInt values throw
    JSON.stringify({ x: 2n });
    // TypeError: BigInt value can't be serialized in JSON
    
    2 can return
    function makeReplacer() {
      let isInitial = true;
    
      return (key, value) => {
        if (isInitial) {
          isInitial = false;
          return value;
        }
        if (key === "") {
          // Omit all properties with name "" (except the initial object)
          return undefined;
        }
        return value;
      };
    }
    
    const replacer = makeReplacer();
    console.log(JSON.stringify({ "": 1, b: 2 }, replacer)); // "{"b":2}"
    
    9 when passing in "pure" values like
    const foo = {
      foundation: "Mozilla",
      model: "box",
      week: 45,
      transport: "car",
      month: 7,
    };
    
    JSON.stringify(foo, ["week", "month"]);
    // '{"week":45,"month":7}', only keep "week" and "month" properties
    
    5 or
    const foo = {
      foundation: "Mozilla",
      model: "box",
      week: 45,
      transport: "car",
      month: 7,
    };
    
    JSON.stringify(foo, ["week", "month"]);
    // '{"week":45,"month":7}', only keep "week" and "month" properties
    
    6.
  • The numbers
    const foo = {
      foundation: "Mozilla",
      model: "box",
      week: 45,
      transport: "car",
      month: 7,
    };
    
    JSON.stringify(foo, ["week", "month"]);
    // '{"week":45,"month":7}', only keep "week" and "month" properties
    
    7 and
    const foo = {
      foundation: "Mozilla",
      model: "box",
      week: 45,
      transport: "car",
      month: 7,
    };
    
    JSON.stringify(foo, ["week", "month"]);
    // '{"week":45,"month":7}', only keep "week" and "month" properties
    
    8, as well as the value
    JSON.stringify({}); // '{}'
    JSON.stringify(true); // 'true'
    JSON.stringify("foo"); // '"foo"'
    JSON.stringify([1, "false", false]); // '[1,"false",false]'
    JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
    JSON.stringify({ x: 5 }); // '{"x":5}'
    
    JSON.stringify(new Date(1906, 0, 2, 15, 4, 5));
    // '"1906-01-02T15:04:05.000Z"'
    
    JSON.stringify({ x: 5, y: 6 });
    // '{"x":5,"y":6}'
    JSON.stringify([new Number(3), new String("false"), new Boolean(false)]);
    // '[3,"false",false]'
    
    // String-keyed array elements are not enumerable and make no sense in JSON
    const a = ["foo", "bar"];
    a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
    JSON.stringify(a);
    // '["foo","bar"]'
    
    JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] });
    // '{"x":[10,null,null,null]}'
    
    // Standard data structures
    JSON.stringify([
      new Set([1]),
      new Map([[1, 2]]),
      new WeakSet([{ a: 1 }]),
      new WeakMap([[{ a: 1 }, 2]]),
    ]);
    // '[{},{},{},{}]'
    
    // TypedArray
    JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
    // '[{"0":1},{"0":1},{"0":1}]'
    JSON.stringify([
      new Uint8Array([1]),
      new Uint8ClampedArray([1]),
      new Uint16Array([1]),
      new Uint32Array([1]),
    ]);
    // '[{"0":1},{"0":1},{"0":1},{"0":1}]'
    JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
    // '[{"0":1},{"0":1}]'
    
    // toJSON()
    JSON.stringify({
      x: 5,
      y: 6,
      toJSON() {
        return this.x + this.y;
      },
    });
    // '11'
    
    // Symbols:
    JSON.stringify({ x: undefined, y: Object, z: Symbol("") });
    // '{}'
    JSON.stringify({ [Symbol("foo")]: "foo" });
    // '{}'
    JSON.stringify({ [Symbol.for("foo")]: "foo" }, [Symbol.for("foo")]);
    // '{}'
    JSON.stringify({ [Symbol.for("foo")]: "foo" }, (k, v) => {
      if (typeof k === "symbol") {
        return "a symbol";
      }
    });
    // undefined
    
    // Non-enumerable properties:
    JSON.stringify(
      Object.create(null, {
        x: { value: "x", enumerable: false },
        y: { value: "y", enumerable: true },
      }),
    );
    // '{"y":"y"}'
    
    // BigInt values throw
    JSON.stringify({ x: 2n });
    // TypeError: BigInt value can't be serialized in JSON
    
    9, are all considered
    JSON.stringify({}); // '{}'
    JSON.stringify(true); // 'true'
    JSON.stringify("foo"); // '"foo"'
    JSON.stringify([1, "false", false]); // '[1,"false",false]'
    JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
    JSON.stringify({ x: 5 }); // '{"x":5}'
    
    JSON.stringify(new Date(1906, 0, 2, 15, 4, 5));
    // '"1906-01-02T15:04:05.000Z"'
    
    JSON.stringify({ x: 5, y: 6 });
    // '{"x":5,"y":6}'
    JSON.stringify([new Number(3), new String("false"), new Boolean(false)]);
    // '[3,"false",false]'
    
    // String-keyed array elements are not enumerable and make no sense in JSON
    const a = ["foo", "bar"];
    a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
    JSON.stringify(a);
    // '["foo","bar"]'
    
    JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] });
    // '{"x":[10,null,null,null]}'
    
    // Standard data structures
    JSON.stringify([
      new Set([1]),
      new Map([[1, 2]]),
      new WeakSet([{ a: 1 }]),
      new WeakMap([[{ a: 1 }, 2]]),
    ]);
    // '[{},{},{},{}]'
    
    // TypedArray
    JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
    // '[{"0":1},{"0":1},{"0":1}]'
    JSON.stringify([
      new Uint8Array([1]),
      new Uint8ClampedArray([1]),
      new Uint16Array([1]),
      new Uint32Array([1]),
    ]);
    // '[{"0":1},{"0":1},{"0":1},{"0":1}]'
    JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
    // '[{"0":1},{"0":1}]'
    
    // toJSON()
    JSON.stringify({
      x: 5,
      y: 6,
      toJSON() {
        return this.x + this.y;
      },
    });
    // '11'
    
    // Symbols:
    JSON.stringify({ x: undefined, y: Object, z: Symbol("") });
    // '{}'
    JSON.stringify({ [Symbol("foo")]: "foo" });
    // '{}'
    JSON.stringify({ [Symbol.for("foo")]: "foo" }, [Symbol.for("foo")]);
    // '{}'
    JSON.stringify({ [Symbol.for("foo")]: "foo" }, (k, v) => {
      if (typeof k === "symbol") {
        return "a symbol";
      }
    });
    // undefined
    
    // Non-enumerable properties:
    JSON.stringify(
      Object.create(null, {
        x: { value: "x", enumerable: false },
        y: { value: "y", enumerable: true },
      }),
    );
    // '{"y":"y"}'
    
    // BigInt values throw
    JSON.stringify({ x: 2n });
    // TypeError: BigInt value can't be serialized in JSON
    
    9. (But unlike the values in the previous point, they would never be omitted.)
  • Arrays are serialized as arrays (enclosed by square brackets). Only array indices between 0 and
    console.log(JSON.stringify({ a: 2 }, null, " "));
    /*
    {
     "a": 2
    }
    */
    
    1 (inclusive) are serialized; other properties are ignored.
  • For other objects:
    • All
      JSON.stringify({}); // '{}'
      JSON.stringify(true); // 'true'
      JSON.stringify("foo"); // '"foo"'
      JSON.stringify([1, "false", false]); // '[1,"false",false]'
      JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
      JSON.stringify({ x: 5 }); // '{"x":5}'
      
      JSON.stringify(new Date(1906, 0, 2, 15, 4, 5));
      // '"1906-01-02T15:04:05.000Z"'
      
      JSON.stringify({ x: 5, y: 6 });
      // '{"x":5,"y":6}'
      JSON.stringify([new Number(3), new String("false"), new Boolean(false)]);
      // '[3,"false",false]'
      
      // String-keyed array elements are not enumerable and make no sense in JSON
      const a = ["foo", "bar"];
      a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
      JSON.stringify(a);
      // '["foo","bar"]'
      
      JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] });
      // '{"x":[10,null,null,null]}'
      
      // Standard data structures
      JSON.stringify([
        new Set([1]),
        new Map([[1, 2]]),
        new WeakSet([{ a: 1 }]),
        new WeakMap([[{ a: 1 }, 2]]),
      ]);
      // '[{},{},{},{}]'
      
      // TypedArray
      JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
      // '[{"0":1},{"0":1},{"0":1}]'
      JSON.stringify([
        new Uint8Array([1]),
        new Uint8ClampedArray([1]),
        new Uint16Array([1]),
        new Uint32Array([1]),
      ]);
      // '[{"0":1},{"0":1},{"0":1},{"0":1}]'
      JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
      // '[{"0":1},{"0":1}]'
      
      // toJSON()
      JSON.stringify({
        x: 5,
        y: 6,
        toJSON() {
          return this.x + this.y;
        },
      });
      // '11'
      
      // Symbols:
      JSON.stringify({ x: undefined, y: Object, z: Symbol("") });
      // '{}'
      JSON.stringify({ [Symbol("foo")]: "foo" });
      // '{}'
      JSON.stringify({ [Symbol.for("foo")]: "foo" }, [Symbol.for("foo")]);
      // '{}'
      JSON.stringify({ [Symbol.for("foo")]: "foo" }, (k, v) => {
        if (typeof k === "symbol") {
          return "a symbol";
        }
      });
      // undefined
      
      // Non-enumerable properties:
      JSON.stringify(
        Object.create(null, {
          x: { value: "x", enumerable: false },
          y: { value: "y", enumerable: true },
        }),
      );
      // '{"y":"y"}'
      
      // BigInt values throw
      JSON.stringify({ x: 2n });
      // TypeError: BigInt value can't be serialized in JSON
      
      7-keyed properties will be completely ignored, even when using the parameter.
    • If the value has a
      function makeReplacer() {
        let isInitial = true;
      
        return (key, value) => {
          if (isInitial) {
            isInitial = false;
            return value;
          }
          if (key === "") {
            // Omit all properties with name "" (except the initial object)
            return undefined;
          }
          return value;
        };
      }
      
      const replacer = makeReplacer();
      console.log(JSON.stringify({ "": 1, b: 2 }, replacer)); // "{"b":2}"
      
      7 method, it's responsible to define what data will be serialized. Instead of the object being serialized, the value returned by the
      function makeReplacer() {
        let isInitial = true;
      
        return (key, value) => {
          if (isInitial) {
            isInitial = false;
            return value;
          }
          if (key === "") {
            // Omit all properties with name "" (except the initial object)
            return undefined;
          }
          return value;
        };
      }
      
      const replacer = makeReplacer();
      console.log(JSON.stringify({ "": 1, b: 2 }, replacer)); // "{"b":2}"
      
      7 method when called will be serialized.
      JSON.stringify({}); // '{}'
      JSON.stringify(true); // 'true'
      JSON.stringify("foo"); // '"foo"'
      JSON.stringify([1, "false", false]); // '[1,"false",false]'
      JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
      JSON.stringify({ x: 5 }); // '{"x":5}'
      
      JSON.stringify(new Date(1906, 0, 2, 15, 4, 5));
      // '"1906-01-02T15:04:05.000Z"'
      
      JSON.stringify({ x: 5, y: 6 });
      // '{"x":5,"y":6}'
      JSON.stringify([new Number(3), new String("false"), new Boolean(false)]);
      // '[3,"false",false]'
      
      // String-keyed array elements are not enumerable and make no sense in JSON
      const a = ["foo", "bar"];
      a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
      JSON.stringify(a);
      // '["foo","bar"]'
      
      JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] });
      // '{"x":[10,null,null,null]}'
      
      // Standard data structures
      JSON.stringify([
        new Set([1]),
        new Map([[1, 2]]),
        new WeakSet([{ a: 1 }]),
        new WeakMap([[{ a: 1 }, 2]]),
      ]);
      // '[{},{},{},{}]'
      
      // TypedArray
      JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
      // '[{"0":1},{"0":1},{"0":1}]'
      JSON.stringify([
        new Uint8Array([1]),
        new Uint8ClampedArray([1]),
        new Uint16Array([1]),
        new Uint32Array([1]),
      ]);
      // '[{"0":1},{"0":1},{"0":1},{"0":1}]'
      JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
      // '[{"0":1},{"0":1}]'
      
      // toJSON()
      JSON.stringify({
        x: 5,
        y: 6,
        toJSON() {
          return this.x + this.y;
        },
      });
      // '11'
      
      // Symbols:
      JSON.stringify({ x: undefined, y: Object, z: Symbol("") });
      // '{}'
      JSON.stringify({ [Symbol("foo")]: "foo" });
      // '{}'
      JSON.stringify({ [Symbol.for("foo")]: "foo" }, [Symbol.for("foo")]);
      // '{}'
      JSON.stringify({ [Symbol.for("foo")]: "foo" }, (k, v) => {
        if (typeof k === "symbol") {
          return "a symbol";
        }
      });
      // undefined
      
      // Non-enumerable properties:
      JSON.stringify(
        Object.create(null, {
          x: { value: "x", enumerable: false },
          y: { value: "y", enumerable: true },
        }),
      );
      // '{"y":"y"}'
      
      // BigInt values throw
      JSON.stringify({ x: 2n });
      // TypeError: BigInt value can't be serialized in JSON
      
      2 calls
      console.log(JSON.stringify({ a: 2 }, null, " "));
      /*
      {
       "a": 2
      }
      */
      
      7 with one parameter, the
      console.log(JSON.stringify({ a: 2 }, null, " "));
      /*
      {
       "a": 2
      }
      */
      
      8, which has the same semantic as the
      console.log(JSON.stringify({ a: 2 }, null, " "));
      /*
      {
       "a": 2
      }
      */
      
      8 parameter of the function:
      • if this object is a property value, the property name
      • if it is in an array, the index in the array, as a string
      • if
        JSON.stringify({}); // '{}'
        JSON.stringify(true); // 'true'
        JSON.stringify("foo"); // '"foo"'
        JSON.stringify([1, "false", false]); // '[1,"false",false]'
        JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
        JSON.stringify({ x: 5 }); // '{"x":5}'
        
        JSON.stringify(new Date(1906, 0, 2, 15, 4, 5));
        // '"1906-01-02T15:04:05.000Z"'
        
        JSON.stringify({ x: 5, y: 6 });
        // '{"x":5,"y":6}'
        JSON.stringify([new Number(3), new String("false"), new Boolean(false)]);
        // '[3,"false",false]'
        
        // String-keyed array elements are not enumerable and make no sense in JSON
        const a = ["foo", "bar"];
        a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
        JSON.stringify(a);
        // '["foo","bar"]'
        
        JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] });
        // '{"x":[10,null,null,null]}'
        
        // Standard data structures
        JSON.stringify([
          new Set([1]),
          new Map([[1, 2]]),
          new WeakSet([{ a: 1 }]),
          new WeakMap([[{ a: 1 }, 2]]),
        ]);
        // '[{},{},{},{}]'
        
        // TypedArray
        JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
        // '[{"0":1},{"0":1},{"0":1}]'
        JSON.stringify([
          new Uint8Array([1]),
          new Uint8ClampedArray([1]),
          new Uint16Array([1]),
          new Uint32Array([1]),
        ]);
        // '[{"0":1},{"0":1},{"0":1},{"0":1}]'
        JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
        // '[{"0":1},{"0":1}]'
        
        // toJSON()
        JSON.stringify({
          x: 5,
          y: 6,
          toJSON() {
            return this.x + this.y;
          },
        });
        // '11'
        
        // Symbols:
        JSON.stringify({ x: undefined, y: Object, z: Symbol("") });
        // '{}'
        JSON.stringify({ [Symbol("foo")]: "foo" });
        // '{}'
        JSON.stringify({ [Symbol.for("foo")]: "foo" }, [Symbol.for("foo")]);
        // '{}'
        JSON.stringify({ [Symbol.for("foo")]: "foo" }, (k, v) => {
          if (typeof k === "symbol") {
            return "a symbol";
          }
        });
        // undefined
        
        // Non-enumerable properties:
        JSON.stringify(
          Object.create(null, {
            x: { value: "x", enumerable: false },
            y: { value: "y", enumerable: true },
          }),
        );
        // '{"y":"y"}'
        
        // BigInt values throw
        JSON.stringify({ x: 2n });
        // TypeError: BigInt value can't be serialized in JSON
        
        2 was directly called on this object, an empty string
      console.log(JSON.stringify({ uno: 1, dos: 2 }, null, "\t"));
      /*
      {
      	"uno": 1,
      	"dos": 2
      }
      */
      
      2 objects implement the
      function makeReplacer() {
        let isInitial = true;
      
        return (key, value) => {
          if (isInitial) {
            isInitial = false;
            return value;
          }
          if (key === "") {
            // Omit all properties with name "" (except the initial object)
            return undefined;
          }
          return value;
        };
      }
      
      const replacer = makeReplacer();
      console.log(JSON.stringify({ "": 1, b: 2 }, replacer)); // "{"b":2}"
      
      7 method which returns a string (the same as
      console.log(JSON.stringify({ uno: 1, dos: 2 }, null, "\t"));
      /*
      {
      	"uno": 1,
      	"dos": 2
      }
      */
      
      4). Thus, they will be stringified as strings.
    • Only enumerable own properties are visited. This means
      console.log(JSON.stringify({ uno: 1, dos: 2 }, null, "\t"));
      /*
      {
      	"uno": 1,
      	"dos": 2
      }
      */
      
      5,
      console.log(JSON.stringify({ uno: 1, dos: 2 }, null, "\t"));
      /*
      {
      	"uno": 1,
      	"dos": 2
      }
      */
      
      6, etc. will become
      console.log(JSON.stringify({ uno: 1, dos: 2 }, null, "\t"));
      /*
      {
      	"uno": 1,
      	"dos": 2
      }
      */
      
      7. You can use the parameter to serialize them to something more useful. Properties are visited using the same algorithm as
      console.log(JSON.stringify({ uno: 1, dos: 2 }, null, "\t"));
      /*
      {
      	"uno": 1,
      	"dos": 2
      }
      */
      
      9, which has a well-defined order and is stable across implementations. For example,
      const obj = {
        data: "data",
      
        toJSON(key) {
          return key ? `Now I am a nested object under key '${key}'` : this;
        },
      };
      
      JSON.stringify(obj);
      // '{"data":"data"}'
      
      JSON.stringify({ obj });
      // '{"obj":"Now I am a nested object under key 'obj'"}'
      
      JSON.stringify([obj]);
      // '["Now I am a nested object under key '0'"]'
      
      0 on the same object will always produce the same string, and
      const obj = {
        data: "data",
      
        toJSON(key) {
          return key ? `Now I am a nested object under key '${key}'` : this;
        },
      };
      
      JSON.stringify(obj);
      // '{"data":"data"}'
      
      JSON.stringify({ obj });
      // '{"obj":"Now I am a nested object under key 'obj'"}'
      
      JSON.stringify([obj]);
      // '["Now I am a nested object under key '0'"]'
      
      1 would produce an object with the same key ordering as the original (assuming the object is completely JSON-serializable).

The

JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(1906, 0, 2, 15, 4, 5));
// '"1906-01-02T15:04:05.000Z"'

JSON.stringify({ x: 5, y: 6 });
// '{"x":5,"y":6}'
JSON.stringify([new Number(3), new String("false"), new Boolean(false)]);
// '[3,"false",false]'

// String-keyed array elements are not enumerable and make no sense in JSON
const a = ["foo", "bar"];
a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
JSON.stringify(a);
// '["foo","bar"]'

JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] });
// '{"x":[10,null,null,null]}'

// Standard data structures
JSON.stringify([
  new Set([1]),
  new Map([[1, 2]]),
  new WeakSet([{ a: 1 }]),
  new WeakMap([[{ a: 1 }, 2]]),
]);
// '[{},{},{},{}]'

// TypedArray
JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
// '[{"0":1},{"0":1},{"0":1}]'
JSON.stringify([
  new Uint8Array([1]),
  new Uint8ClampedArray([1]),
  new Uint16Array([1]),
  new Uint32Array([1]),
]);
// '[{"0":1},{"0":1},{"0":1},{"0":1}]'
JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
// '[{"0":1},{"0":1}]'

// toJSON()
JSON.stringify({
  x: 5,
  y: 6,
  toJSON() {
    return this.x + this.y;
  },
});
// '11'

// Symbols:
JSON.stringify({ x: undefined, y: Object, z: Symbol("") });
// '{}'
JSON.stringify({ [Symbol("foo")]: "foo" });
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, [Symbol.for("foo")]);
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

// Non-enumerable properties:
JSON.stringify(
  Object.create(null, {
    x: { value: "x", enumerable: false },
    y: { value: "y", enumerable: true },
  }),
);
// '{"y":"y"}'

// BigInt values throw
JSON.stringify({ x: 2n });
// TypeError: BigInt value can't be serialized in JSON
4 parameter can be either a function or an array.

As an array, its elements indicate the names of the properties in the object that should be included in the resulting JSON string. Only string and number values are taken into account; symbol keys are ignored.

As a function, it takes two parameters: the

console.log(JSON.stringify({ a: 2 }, null, " "));
/*
{
 "a": 2
}
*/
8 and the
JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(1906, 0, 2, 15, 4, 5));
// '"1906-01-02T15:04:05.000Z"'

JSON.stringify({ x: 5, y: 6 });
// '{"x":5,"y":6}'
JSON.stringify([new Number(3), new String("false"), new Boolean(false)]);
// '[3,"false",false]'

// String-keyed array elements are not enumerable and make no sense in JSON
const a = ["foo", "bar"];
a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
JSON.stringify(a);
// '["foo","bar"]'

JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] });
// '{"x":[10,null,null,null]}'

// Standard data structures
JSON.stringify([
  new Set([1]),
  new Map([[1, 2]]),
  new WeakSet([{ a: 1 }]),
  new WeakMap([[{ a: 1 }, 2]]),
]);
// '[{},{},{},{}]'

// TypedArray
JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
// '[{"0":1},{"0":1},{"0":1}]'
JSON.stringify([
  new Uint8Array([1]),
  new Uint8ClampedArray([1]),
  new Uint16Array([1]),
  new Uint32Array([1]),
]);
// '[{"0":1},{"0":1},{"0":1},{"0":1}]'
JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
// '[{"0":1},{"0":1}]'

// toJSON()
JSON.stringify({
  x: 5,
  y: 6,
  toJSON() {
    return this.x + this.y;
  },
});
// '11'

// Symbols:
JSON.stringify({ x: undefined, y: Object, z: Symbol("") });
// '{}'
JSON.stringify({ [Symbol("foo")]: "foo" });
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, [Symbol.for("foo")]);
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

// Non-enumerable properties:
JSON.stringify(
  Object.create(null, {
    x: { value: "x", enumerable: false },
    y: { value: "y", enumerable: true },
  }),
);
// '{"y":"y"}'

// BigInt values throw
JSON.stringify({ x: 2n });
// TypeError: BigInt value can't be serialized in JSON
3 being stringified. The object in which the key was found is provided as the
JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(1906, 0, 2, 15, 4, 5));
// '"1906-01-02T15:04:05.000Z"'

JSON.stringify({ x: 5, y: 6 });
// '{"x":5,"y":6}'
JSON.stringify([new Number(3), new String("false"), new Boolean(false)]);
// '[3,"false",false]'

// String-keyed array elements are not enumerable and make no sense in JSON
const a = ["foo", "bar"];
a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
JSON.stringify(a);
// '["foo","bar"]'

JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] });
// '{"x":[10,null,null,null]}'

// Standard data structures
JSON.stringify([
  new Set([1]),
  new Map([[1, 2]]),
  new WeakSet([{ a: 1 }]),
  new WeakMap([[{ a: 1 }, 2]]),
]);
// '[{},{},{},{}]'

// TypedArray
JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
// '[{"0":1},{"0":1},{"0":1}]'
JSON.stringify([
  new Uint8Array([1]),
  new Uint8ClampedArray([1]),
  new Uint16Array([1]),
  new Uint32Array([1]),
]);
// '[{"0":1},{"0":1},{"0":1},{"0":1}]'
JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
// '[{"0":1},{"0":1}]'

// toJSON()
JSON.stringify({
  x: 5,
  y: 6,
  toJSON() {
    return this.x + this.y;
  },
});
// '11'

// Symbols:
JSON.stringify({ x: undefined, y: Object, z: Symbol("") });
// '{}'
JSON.stringify({ [Symbol("foo")]: "foo" });
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, [Symbol.for("foo")]);
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

// Non-enumerable properties:
JSON.stringify(
  Object.create(null, {
    x: { value: "x", enumerable: false },
    y: { value: "y", enumerable: true },
  }),
);
// '{"y":"y"}'

// BigInt values throw
JSON.stringify({ x: 2n });
// TypeError: BigInt value can't be serialized in JSON
4's
const obj = {
  data: "data",

  toJSON(key) {
    return key ? `Now I am a nested object under key '${key}'` : this;
  },
};

JSON.stringify(obj);
// '{"data":"data"}'

JSON.stringify({ obj });
// '{"obj":"Now I am a nested object under key 'obj'"}'

JSON.stringify([obj]);
// '["Now I am a nested object under key '0'"]'
6 context.

The

JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(1906, 0, 2, 15, 4, 5));
// '"1906-01-02T15:04:05.000Z"'

JSON.stringify({ x: 5, y: 6 });
// '{"x":5,"y":6}'
JSON.stringify([new Number(3), new String("false"), new Boolean(false)]);
// '[3,"false",false]'

// String-keyed array elements are not enumerable and make no sense in JSON
const a = ["foo", "bar"];
a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
JSON.stringify(a);
// '["foo","bar"]'

JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] });
// '{"x":[10,null,null,null]}'

// Standard data structures
JSON.stringify([
  new Set([1]),
  new Map([[1, 2]]),
  new WeakSet([{ a: 1 }]),
  new WeakMap([[{ a: 1 }, 2]]),
]);
// '[{},{},{},{}]'

// TypedArray
JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
// '[{"0":1},{"0":1},{"0":1}]'
JSON.stringify([
  new Uint8Array([1]),
  new Uint8ClampedArray([1]),
  new Uint16Array([1]),
  new Uint32Array([1]),
]);
// '[{"0":1},{"0":1},{"0":1},{"0":1}]'
JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
// '[{"0":1},{"0":1}]'

// toJSON()
JSON.stringify({
  x: 5,
  y: 6,
  toJSON() {
    return this.x + this.y;
  },
});
// '11'

// Symbols:
JSON.stringify({ x: undefined, y: Object, z: Symbol("") });
// '{}'
JSON.stringify({ [Symbol("foo")]: "foo" });
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, [Symbol.for("foo")]);
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

// Non-enumerable properties:
JSON.stringify(
  Object.create(null, {
    x: { value: "x", enumerable: false },
    y: { value: "y", enumerable: true },
  }),
);
// '{"y":"y"}'

// BigInt values throw
JSON.stringify({ x: 2n });
// TypeError: BigInt value can't be serialized in JSON
4 function is called for the initial object being stringified as well, in which case the
console.log(JSON.stringify({ a: 2 }, null, " "));
/*
{
 "a": 2
}
*/
8 is an empty string (
const obj = {
  data: "data",

  toJSON(key) {
    return key ? `Now I am a nested object under key '${key}'` : this;
  },
};

JSON.stringify(obj);
// '{"data":"data"}'

JSON.stringify({ obj });
// '{"obj":"Now I am a nested object under key 'obj'"}'

JSON.stringify([obj]);
// '["Now I am a nested object under key '0'"]'
9). It is then called for each property on the object or array being stringified. Array indices will be provided in its string form as
console.log(JSON.stringify({ a: 2 }, null, " "));
/*
{
 "a": 2
}
*/
8. The current property value will be replaced with the
JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(1906, 0, 2, 15, 4, 5));
// '"1906-01-02T15:04:05.000Z"'

JSON.stringify({ x: 5, y: 6 });
// '{"x":5,"y":6}'
JSON.stringify([new Number(3), new String("false"), new Boolean(false)]);
// '[3,"false",false]'

// String-keyed array elements are not enumerable and make no sense in JSON
const a = ["foo", "bar"];
a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
JSON.stringify(a);
// '["foo","bar"]'

JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] });
// '{"x":[10,null,null,null]}'

// Standard data structures
JSON.stringify([
  new Set([1]),
  new Map([[1, 2]]),
  new WeakSet([{ a: 1 }]),
  new WeakMap([[{ a: 1 }, 2]]),
]);
// '[{},{},{},{}]'

// TypedArray
JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
// '[{"0":1},{"0":1},{"0":1}]'
JSON.stringify([
  new Uint8Array([1]),
  new Uint8ClampedArray([1]),
  new Uint16Array([1]),
  new Uint32Array([1]),
]);
// '[{"0":1},{"0":1},{"0":1},{"0":1}]'
JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
// '[{"0":1},{"0":1}]'

// toJSON()
JSON.stringify({
  x: 5,
  y: 6,
  toJSON() {
    return this.x + this.y;
  },
});
// '11'

// Symbols:
JSON.stringify({ x: undefined, y: Object, z: Symbol("") });
// '{}'
JSON.stringify({ [Symbol("foo")]: "foo" });
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, [Symbol.for("foo")]);
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

// Non-enumerable properties:
JSON.stringify(
  Object.create(null, {
    x: { value: "x", enumerable: false },
    y: { value: "y", enumerable: true },
  }),
);
// '{"y":"y"}'

// BigInt values throw
JSON.stringify({ x: 2n });
// TypeError: BigInt value can't be serialized in JSON
4's return value for stringification. This means:

  • If you return a number, string, boolean, or
    JSON.stringify({}); // '{}'
    JSON.stringify(true); // 'true'
    JSON.stringify("foo"); // '"foo"'
    JSON.stringify([1, "false", false]); // '[1,"false",false]'
    JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
    JSON.stringify({ x: 5 }); // '{"x":5}'
    
    JSON.stringify(new Date(1906, 0, 2, 15, 4, 5));
    // '"1906-01-02T15:04:05.000Z"'
    
    JSON.stringify({ x: 5, y: 6 });
    // '{"x":5,"y":6}'
    JSON.stringify([new Number(3), new String("false"), new Boolean(false)]);
    // '[3,"false",false]'
    
    // String-keyed array elements are not enumerable and make no sense in JSON
    const a = ["foo", "bar"];
    a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
    JSON.stringify(a);
    // '["foo","bar"]'
    
    JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] });
    // '{"x":[10,null,null,null]}'
    
    // Standard data structures
    JSON.stringify([
      new Set([1]),
      new Map([[1, 2]]),
      new WeakSet([{ a: 1 }]),
      new WeakMap([[{ a: 1 }, 2]]),
    ]);
    // '[{},{},{},{}]'
    
    // TypedArray
    JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
    // '[{"0":1},{"0":1},{"0":1}]'
    JSON.stringify([
      new Uint8Array([1]),
      new Uint8ClampedArray([1]),
      new Uint16Array([1]),
      new Uint32Array([1]),
    ]);
    // '[{"0":1},{"0":1},{"0":1},{"0":1}]'
    JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
    // '[{"0":1},{"0":1}]'
    
    // toJSON()
    JSON.stringify({
      x: 5,
      y: 6,
      toJSON() {
        return this.x + this.y;
      },
    });
    // '11'
    
    // Symbols:
    JSON.stringify({ x: undefined, y: Object, z: Symbol("") });
    // '{}'
    JSON.stringify({ [Symbol("foo")]: "foo" });
    // '{}'
    JSON.stringify({ [Symbol.for("foo")]: "foo" }, [Symbol.for("foo")]);
    // '{}'
    JSON.stringify({ [Symbol.for("foo")]: "foo" }, (k, v) => {
      if (typeof k === "symbol") {
        return "a symbol";
      }
    });
    // undefined
    
    // Non-enumerable properties:
    JSON.stringify(
      Object.create(null, {
        x: { value: "x", enumerable: false },
        y: { value: "y", enumerable: true },
      }),
    );
    // '{"y":"y"}'
    
    // BigInt values throw
    JSON.stringify({ x: 2n });
    // TypeError: BigInt value can't be serialized in JSON
    
    9, that value is directly serialized and used as the property's value. (Returning a BigInt will throw as well.)
  • If you return a
    const foo = {
      foundation: "Mozilla",
      model: "box",
      week: 45,
      transport: "car",
      month: 7,
    };
    
    JSON.stringify(foo, ["week", "month"]);
    // '{"week":45,"month":7}', only keep "week" and "month" properties
    
    0,
    JSON.stringify({}); // '{}'
    JSON.stringify(true); // 'true'
    JSON.stringify("foo"); // '"foo"'
    JSON.stringify([1, "false", false]); // '[1,"false",false]'
    JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
    JSON.stringify({ x: 5 }); // '{"x":5}'
    
    JSON.stringify(new Date(1906, 0, 2, 15, 4, 5));
    // '"1906-01-02T15:04:05.000Z"'
    
    JSON.stringify({ x: 5, y: 6 });
    // '{"x":5,"y":6}'
    JSON.stringify([new Number(3), new String("false"), new Boolean(false)]);
    // '[3,"false",false]'
    
    // String-keyed array elements are not enumerable and make no sense in JSON
    const a = ["foo", "bar"];
    a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
    JSON.stringify(a);
    // '["foo","bar"]'
    
    JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] });
    // '{"x":[10,null,null,null]}'
    
    // Standard data structures
    JSON.stringify([
      new Set([1]),
      new Map([[1, 2]]),
      new WeakSet([{ a: 1 }]),
      new WeakMap([[{ a: 1 }, 2]]),
    ]);
    // '[{},{},{},{}]'
    
    // TypedArray
    JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
    // '[{"0":1},{"0":1},{"0":1}]'
    JSON.stringify([
      new Uint8Array([1]),
      new Uint8ClampedArray([1]),
      new Uint16Array([1]),
      new Uint32Array([1]),
    ]);
    // '[{"0":1},{"0":1},{"0":1},{"0":1}]'
    JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
    // '[{"0":1},{"0":1}]'
    
    // toJSON()
    JSON.stringify({
      x: 5,
      y: 6,
      toJSON() {
        return this.x + this.y;
      },
    });
    // '11'
    
    // Symbols:
    JSON.stringify({ x: undefined, y: Object, z: Symbol("") });
    // '{}'
    JSON.stringify({ [Symbol("foo")]: "foo" });
    // '{}'
    JSON.stringify({ [Symbol.for("foo")]: "foo" }, [Symbol.for("foo")]);
    // '{}'
    JSON.stringify({ [Symbol.for("foo")]: "foo" }, (k, v) => {
      if (typeof k === "symbol") {
        return "a symbol";
      }
    });
    // undefined
    
    // Non-enumerable properties:
    JSON.stringify(
      Object.create(null, {
        x: { value: "x", enumerable: false },
        y: { value: "y", enumerable: true },
      }),
    );
    // '{"y":"y"}'
    
    // BigInt values throw
    JSON.stringify({ x: 2n });
    // TypeError: BigInt value can't be serialized in JSON
    
    7, or
    function makeReplacer() {
      let isInitial = true;
    
      return (key, value) => {
        if (isInitial) {
          isInitial = false;
          return value;
        }
        if (key === "") {
          // Omit all properties with name "" (except the initial object)
          return undefined;
        }
        return value;
      };
    }
    
    const replacer = makeReplacer();
    console.log(JSON.stringify({ "": 1, b: 2 }, replacer)); // "{"b":2}"
    
    9, the property is not included in the output.
  • If you return any other object, the object is recursively stringified, calling the
    JSON.stringify({}); // '{}'
    JSON.stringify(true); // 'true'
    JSON.stringify("foo"); // '"foo"'
    JSON.stringify([1, "false", false]); // '[1,"false",false]'
    JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
    JSON.stringify({ x: 5 }); // '{"x":5}'
    
    JSON.stringify(new Date(1906, 0, 2, 15, 4, 5));
    // '"1906-01-02T15:04:05.000Z"'
    
    JSON.stringify({ x: 5, y: 6 });
    // '{"x":5,"y":6}'
    JSON.stringify([new Number(3), new String("false"), new Boolean(false)]);
    // '[3,"false",false]'
    
    // String-keyed array elements are not enumerable and make no sense in JSON
    const a = ["foo", "bar"];
    a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
    JSON.stringify(a);
    // '["foo","bar"]'
    
    JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] });
    // '{"x":[10,null,null,null]}'
    
    // Standard data structures
    JSON.stringify([
      new Set([1]),
      new Map([[1, 2]]),
      new WeakSet([{ a: 1 }]),
      new WeakMap([[{ a: 1 }, 2]]),
    ]);
    // '[{},{},{},{}]'
    
    // TypedArray
    JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
    // '[{"0":1},{"0":1},{"0":1}]'
    JSON.stringify([
      new Uint8Array([1]),
      new Uint8ClampedArray([1]),
      new Uint16Array([1]),
      new Uint32Array([1]),
    ]);
    // '[{"0":1},{"0":1},{"0":1},{"0":1}]'
    JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
    // '[{"0":1},{"0":1}]'
    
    // toJSON()
    JSON.stringify({
      x: 5,
      y: 6,
      toJSON() {
        return this.x + this.y;
      },
    });
    // '11'
    
    // Symbols:
    JSON.stringify({ x: undefined, y: Object, z: Symbol("") });
    // '{}'
    JSON.stringify({ [Symbol("foo")]: "foo" });
    // '{}'
    JSON.stringify({ [Symbol.for("foo")]: "foo" }, [Symbol.for("foo")]);
    // '{}'
    JSON.stringify({ [Symbol.for("foo")]: "foo" }, (k, v) => {
      if (typeof k === "symbol") {
        return "a symbol";
      }
    });
    // undefined
    
    // Non-enumerable properties:
    JSON.stringify(
      Object.create(null, {
        x: { value: "x", enumerable: false },
        y: { value: "y", enumerable: true },
      }),
    );
    // '{"y":"y"}'
    
    // BigInt values throw
    JSON.stringify({ x: 2n });
    // TypeError: BigInt value can't be serialized in JSON
    
    4 function on each property.

Note: When parsing JSON generated with

JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(1906, 0, 2, 15, 4, 5));
// '"1906-01-02T15:04:05.000Z"'

JSON.stringify({ x: 5, y: 6 });
// '{"x":5,"y":6}'
JSON.stringify([new Number(3), new String("false"), new Boolean(false)]);
// '[3,"false",false]'

// String-keyed array elements are not enumerable and make no sense in JSON
const a = ["foo", "bar"];
a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
JSON.stringify(a);
// '["foo","bar"]'

JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] });
// '{"x":[10,null,null,null]}'

// Standard data structures
JSON.stringify([
  new Set([1]),
  new Map([[1, 2]]),
  new WeakSet([{ a: 1 }]),
  new WeakMap([[{ a: 1 }, 2]]),
]);
// '[{},{},{},{}]'

// TypedArray
JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
// '[{"0":1},{"0":1},{"0":1}]'
JSON.stringify([
  new Uint8Array([1]),
  new Uint8ClampedArray([1]),
  new Uint16Array([1]),
  new Uint32Array([1]),
]);
// '[{"0":1},{"0":1},{"0":1},{"0":1}]'
JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
// '[{"0":1},{"0":1}]'

// toJSON()
JSON.stringify({
  x: 5,
  y: 6,
  toJSON() {
    return this.x + this.y;
  },
});
// '11'

// Symbols:
JSON.stringify({ x: undefined, y: Object, z: Symbol("") });
// '{}'
JSON.stringify({ [Symbol("foo")]: "foo" });
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, [Symbol.for("foo")]);
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

// Non-enumerable properties:
JSON.stringify(
  Object.create(null, {
    x: { value: "x", enumerable: false },
    y: { value: "y", enumerable: true },
  }),
);
// '{"y":"y"}'

// BigInt values throw
JSON.stringify({ x: 2n });
// TypeError: BigInt value can't be serialized in JSON
4 functions, you would likely want to use the parameter to perform the reverse operation.

Typically, array elements' index would never shift (even when the element is an invalid value like a function, it will become

JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(1906, 0, 2, 15, 4, 5));
// '"1906-01-02T15:04:05.000Z"'

JSON.stringify({ x: 5, y: 6 });
// '{"x":5,"y":6}'
JSON.stringify([new Number(3), new String("false"), new Boolean(false)]);
// '[3,"false",false]'

// String-keyed array elements are not enumerable and make no sense in JSON
const a = ["foo", "bar"];
a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
JSON.stringify(a);
// '["foo","bar"]'

JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] });
// '{"x":[10,null,null,null]}'

// Standard data structures
JSON.stringify([
  new Set([1]),
  new Map([[1, 2]]),
  new WeakSet([{ a: 1 }]),
  new WeakMap([[{ a: 1 }, 2]]),
]);
// '[{},{},{},{}]'

// TypedArray
JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
// '[{"0":1},{"0":1},{"0":1}]'
JSON.stringify([
  new Uint8Array([1]),
  new Uint8ClampedArray([1]),
  new Uint16Array([1]),
  new Uint32Array([1]),
]);
// '[{"0":1},{"0":1},{"0":1},{"0":1}]'
JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
// '[{"0":1},{"0":1}]'

// toJSON()
JSON.stringify({
  x: 5,
  y: 6,
  toJSON() {
    return this.x + this.y;
  },
});
// '11'

// Symbols:
JSON.stringify({ x: undefined, y: Object, z: Symbol("") });
// '{}'
JSON.stringify({ [Symbol("foo")]: "foo" });
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, [Symbol.for("foo")]);
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

// Non-enumerable properties:
JSON.stringify(
  Object.create(null, {
    x: { value: "x", enumerable: false },
    y: { value: "y", enumerable: true },
  }),
);
// '{"y":"y"}'

// BigInt values throw
JSON.stringify({ x: 2n });
// TypeError: BigInt value can't be serialized in JSON
9 instead of omitted). Using the
JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(1906, 0, 2, 15, 4, 5));
// '"1906-01-02T15:04:05.000Z"'

JSON.stringify({ x: 5, y: 6 });
// '{"x":5,"y":6}'
JSON.stringify([new Number(3), new String("false"), new Boolean(false)]);
// '[3,"false",false]'

// String-keyed array elements are not enumerable and make no sense in JSON
const a = ["foo", "bar"];
a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
JSON.stringify(a);
// '["foo","bar"]'

JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] });
// '{"x":[10,null,null,null]}'

// Standard data structures
JSON.stringify([
  new Set([1]),
  new Map([[1, 2]]),
  new WeakSet([{ a: 1 }]),
  new WeakMap([[{ a: 1 }, 2]]),
]);
// '[{},{},{},{}]'

// TypedArray
JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
// '[{"0":1},{"0":1},{"0":1}]'
JSON.stringify([
  new Uint8Array([1]),
  new Uint8ClampedArray([1]),
  new Uint16Array([1]),
  new Uint32Array([1]),
]);
// '[{"0":1},{"0":1},{"0":1},{"0":1}]'
JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
// '[{"0":1},{"0":1}]'

// toJSON()
JSON.stringify({
  x: 5,
  y: 6,
  toJSON() {
    return this.x + this.y;
  },
});
// '11'

// Symbols:
JSON.stringify({ x: undefined, y: Object, z: Symbol("") });
// '{}'
JSON.stringify({ [Symbol("foo")]: "foo" });
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, [Symbol.for("foo")]);
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

// Non-enumerable properties:
JSON.stringify(
  Object.create(null, {
    x: { value: "x", enumerable: false },
    y: { value: "y", enumerable: true },
  }),
);
// '{"y":"y"}'

// BigInt values throw
JSON.stringify({ x: 2n });
// TypeError: BigInt value can't be serialized in JSON
4 function allows you to control the order of the array elements by returning a different array.

The

function replacer(key, value) {
  // Filtering out properties
  if (typeof value === "string") {
    return undefined;
  }
  return value;
}

const foo = {
  foundation: "Mozilla",
  model: "box",
  week: 45,
  transport: "car",
  month: 7,
};
JSON.stringify(foo, replacer);
// '{"week":45,"month":7}'
0 parameter may be used to control spacing in the final string.

  • If it is a number, successive levels in the stringification will each be indented by this many space characters.
  • If it is a string, successive levels will be indented by this string.

Each level of indentation will never be longer than 10. Number values of

function replacer(key, value) {
  // Filtering out properties
  if (typeof value === "string") {
    return undefined;
  }
  return value;
}

const foo = {
  foundation: "Mozilla",
  model: "box",
  week: 45,
  transport: "car",
  month: 7,
};
JSON.stringify(foo, replacer);
// '{"week":45,"month":7}'
0 are clamped to 10, and string values are truncated to 10 characters.

JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(1906, 0, 2, 15, 4, 5));
// '"1906-01-02T15:04:05.000Z"'

JSON.stringify({ x: 5, y: 6 });
// '{"x":5,"y":6}'
JSON.stringify([new Number(3), new String("false"), new Boolean(false)]);
// '[3,"false",false]'

// String-keyed array elements are not enumerable and make no sense in JSON
const a = ["foo", "bar"];
a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
JSON.stringify(a);
// '["foo","bar"]'

JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] });
// '{"x":[10,null,null,null]}'

// Standard data structures
JSON.stringify([
  new Set([1]),
  new Map([[1, 2]]),
  new WeakSet([{ a: 1 }]),
  new WeakMap([[{ a: 1 }, 2]]),
]);
// '[{},{},{},{}]'

// TypedArray
JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
// '[{"0":1},{"0":1},{"0":1}]'
JSON.stringify([
  new Uint8Array([1]),
  new Uint8ClampedArray([1]),
  new Uint16Array([1]),
  new Uint32Array([1]),
]);
// '[{"0":1},{"0":1},{"0":1},{"0":1}]'
JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
// '[{"0":1},{"0":1}]'

// toJSON()
JSON.stringify({
  x: 5,
  y: 6,
  toJSON() {
    return this.x + this.y;
  },
});
// '11'

// Symbols:
JSON.stringify({ x: undefined, y: Object, z: Symbol("") });
// '{}'
JSON.stringify({ [Symbol("foo")]: "foo" });
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, [Symbol.for("foo")]);
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

// Non-enumerable properties:
JSON.stringify(
  Object.create(null, {
    x: { value: "x", enumerable: false },
    y: { value: "y", enumerable: true },
  }),
);
// '{"y":"y"}'

// BigInt values throw
JSON.stringify({ x: 2n });
// TypeError: BigInt value can't be serialized in JSON

function replacer(key, value) {
  // Filtering out properties
  if (typeof value === "string") {
    return undefined;
  }
  return value;
}

const foo = {
  foundation: "Mozilla",
  model: "box",
  week: 45,
  transport: "car",
  month: 7,
};
JSON.stringify(foo, replacer);
// '{"week":45,"month":7}'

If you wish the

JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(1906, 0, 2, 15, 4, 5));
// '"1906-01-02T15:04:05.000Z"'

JSON.stringify({ x: 5, y: 6 });
// '{"x":5,"y":6}'
JSON.stringify([new Number(3), new String("false"), new Boolean(false)]);
// '[3,"false",false]'

// String-keyed array elements are not enumerable and make no sense in JSON
const a = ["foo", "bar"];
a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
JSON.stringify(a);
// '["foo","bar"]'

JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] });
// '{"x":[10,null,null,null]}'

// Standard data structures
JSON.stringify([
  new Set([1]),
  new Map([[1, 2]]),
  new WeakSet([{ a: 1 }]),
  new WeakMap([[{ a: 1 }, 2]]),
]);
// '[{},{},{},{}]'

// TypedArray
JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
// '[{"0":1},{"0":1},{"0":1}]'
JSON.stringify([
  new Uint8Array([1]),
  new Uint8ClampedArray([1]),
  new Uint16Array([1]),
  new Uint32Array([1]),
]);
// '[{"0":1},{"0":1},{"0":1},{"0":1}]'
JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
// '[{"0":1},{"0":1}]'

// toJSON()
JSON.stringify({
  x: 5,
  y: 6,
  toJSON() {
    return this.x + this.y;
  },
});
// '11'

// Symbols:
JSON.stringify({ x: undefined, y: Object, z: Symbol("") });
// '{}'
JSON.stringify({ [Symbol("foo")]: "foo" });
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, [Symbol.for("foo")]);
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

// Non-enumerable properties:
JSON.stringify(
  Object.create(null, {
    x: { value: "x", enumerable: false },
    y: { value: "y", enumerable: true },
  }),
);
// '{"y":"y"}'

// BigInt values throw
JSON.stringify({ x: 2n });
// TypeError: BigInt value can't be serialized in JSON
4 to distinguish an initial object from a key with an empty string property (since both would give the empty string as key and potentially an object as value), you will have to keep track of the iteration count (if it is beyond the first iteration, it is a genuine empty string key).

function makeReplacer() {
  let isInitial = true;

  return (key, value) => {
    if (isInitial) {
      isInitial = false;
      return value;
    }
    if (key === "") {
      // Omit all properties with name "" (except the initial object)
      return undefined;
    }
    return value;
  };
}

const replacer = makeReplacer();
console.log(JSON.stringify({ "": 1, b: 2 }, replacer)); // "{"b":2}"

const foo = {
  foundation: "Mozilla",
  model: "box",
  week: 45,
  transport: "car",
  month: 7,
};

JSON.stringify(foo, ["week", "month"]);
// '{"week":45,"month":7}', only keep "week" and "month" properties

Indent the output with one space:

console.log(JSON.stringify({ a: 2 }, null, " "));
/*
{
 "a": 2
}
*/

Using a tab character mimics standard pretty-print appearance:

console.log(JSON.stringify({ uno: 1, dos: 2 }, null, "\t"));
/*
{
	"uno": 1,
	"dos": 2
}
*/

Defining

function makeReplacer() {
  let isInitial = true;

  return (key, value) => {
    if (isInitial) {
      isInitial = false;
      return value;
    }
    if (key === "") {
      // Omit all properties with name "" (except the initial object)
      return undefined;
    }
    return value;
  };
}

const replacer = makeReplacer();
console.log(JSON.stringify({ "": 1, b: 2 }, replacer)); // "{"b":2}"
7 for an object allows overriding its serialization behavior.

const obj = {
  data: "data",

  toJSON(key) {
    return key ? `Now I am a nested object under key '${key}'` : this;
  },
};

JSON.stringify(obj);
// '{"data":"data"}'

JSON.stringify({ obj });
// '{"obj":"Now I am a nested object under key 'obj'"}'

JSON.stringify([obj]);
// '["Now I am a nested object under key '0'"]'

Since the JSON format doesn't support object references (although an IETF draft exists), a

function replacer(key, value) {
  // Filtering out properties
  if (typeof value === "string") {
    return undefined;
  }
  return value;
}

const foo = {
  foundation: "Mozilla",
  model: "box",
  week: 45,
  transport: "car",
  month: 7,
};
JSON.stringify(foo, replacer);
// '{"week":45,"month":7}'
5 will be thrown if one attempts to encode an object with circular references.

const circularReference = {};
circularReference.myself = circularReference;

// Serializing circular references throws "TypeError: cyclic object value"
JSON.stringify(circularReference);

To serialize circular references, you can use a library that supports them (e.g. cycle.js by Douglas Crockford) or implement a solution yourself, which will require finding and replacing (or removing) the cyclic references by serializable values.

If you are using

JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(1906, 0, 2, 15, 4, 5));
// '"1906-01-02T15:04:05.000Z"'

JSON.stringify({ x: 5, y: 6 });
// '{"x":5,"y":6}'
JSON.stringify([new Number(3), new String("false"), new Boolean(false)]);
// '[3,"false",false]'

// String-keyed array elements are not enumerable and make no sense in JSON
const a = ["foo", "bar"];
a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
JSON.stringify(a);
// '["foo","bar"]'

JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] });
// '{"x":[10,null,null,null]}'

// Standard data structures
JSON.stringify([
  new Set([1]),
  new Map([[1, 2]]),
  new WeakSet([{ a: 1 }]),
  new WeakMap([[{ a: 1 }, 2]]),
]);
// '[{},{},{},{}]'

// TypedArray
JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
// '[{"0":1},{"0":1},{"0":1}]'
JSON.stringify([
  new Uint8Array([1]),
  new Uint8ClampedArray([1]),
  new Uint16Array([1]),
  new Uint32Array([1]),
]);
// '[{"0":1},{"0":1},{"0":1},{"0":1}]'
JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
// '[{"0":1},{"0":1}]'

// toJSON()
JSON.stringify({
  x: 5,
  y: 6,
  toJSON() {
    return this.x + this.y;
  },
});
// '11'

// Symbols:
JSON.stringify({ x: undefined, y: Object, z: Symbol("") });
// '{}'
JSON.stringify({ [Symbol("foo")]: "foo" });
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, [Symbol.for("foo")]);
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

// Non-enumerable properties:
JSON.stringify(
  Object.create(null, {
    x: { value: "x", enumerable: false },
    y: { value: "y", enumerable: true },
  }),
);
// '{"y":"y"}'

// BigInt values throw
JSON.stringify({ x: 2n });
// TypeError: BigInt value can't be serialized in JSON
2 to deep-copy an object, you may instead want to use
// Creating an example of JSON
const session = {
  screens: [],
  state: true,
};
session.screens.push({ name: "screenA", width: 450, height: 250 });
session.screens.push({ name: "screenB", width: 650, height: 350 });
session.screens.push({ name: "screenC", width: 750, height: 120 });
session.screens.push({ name: "screenD", width: 250, height: 60 });
session.screens.push({ name: "screenE", width: 390, height: 120 });
session.screens.push({ name: "screenF", width: 1240, height: 650 });

// Converting the JSON string with JSON.stringify()
// then saving with localStorage in the name of session
localStorage.setItem("session", JSON.stringify(session));

// Example of how to transform the String generated through
// JSON.stringify() and saved in localStorage in JSON object again
const restoredSession = JSON.parse(localStorage.getItem("session"));

// Now restoredSession variable contains the object that was saved
// in localStorage
console.log(restoredSession);
7, which supports circular references. JavaScript engine APIs for binary serialization, such as , also support circular references.

In a case where you want to store an object created by your user and allow it to be restored even after the browser has been closed, the following example is a model for the applicability of

JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(1906, 0, 2, 15, 4, 5));
// '"1906-01-02T15:04:05.000Z"'

JSON.stringify({ x: 5, y: 6 });
// '{"x":5,"y":6}'
JSON.stringify([new Number(3), new String("false"), new Boolean(false)]);
// '[3,"false",false]'

// String-keyed array elements are not enumerable and make no sense in JSON
const a = ["foo", "bar"];
a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
JSON.stringify(a);
// '["foo","bar"]'

JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] });
// '{"x":[10,null,null,null]}'

// Standard data structures
JSON.stringify([
  new Set([1]),
  new Map([[1, 2]]),
  new WeakSet([{ a: 1 }]),
  new WeakMap([[{ a: 1 }, 2]]),
]);
// '[{},{},{},{}]'

// TypedArray
JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
// '[{"0":1},{"0":1},{"0":1}]'
JSON.stringify([
  new Uint8Array([1]),
  new Uint8ClampedArray([1]),
  new Uint16Array([1]),
  new Uint32Array([1]),
]);
// '[{"0":1},{"0":1},{"0":1},{"0":1}]'
JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
// '[{"0":1},{"0":1}]'

// toJSON()
JSON.stringify({
  x: 5,
  y: 6,
  toJSON() {
    return this.x + this.y;
  },
});
// '11'

// Symbols:
JSON.stringify({ x: undefined, y: Object, z: Symbol("") });
// '{}'
JSON.stringify({ [Symbol("foo")]: "foo" });
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, [Symbol.for("foo")]);
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

// Non-enumerable properties:
JSON.stringify(
  Object.create(null, {
    x: { value: "x", enumerable: false },
    y: { value: "y", enumerable: true },
  }),
);
// '{"y":"y"}'

// BigInt values throw
JSON.stringify({ x: 2n });
// TypeError: BigInt value can't be serialized in JSON
2:

// Creating an example of JSON
const session = {
  screens: [],
  state: true,
};
session.screens.push({ name: "screenA", width: 450, height: 250 });
session.screens.push({ name: "screenB", width: 650, height: 350 });
session.screens.push({ name: "screenC", width: 750, height: 120 });
session.screens.push({ name: "screenD", width: 250, height: 60 });
session.screens.push({ name: "screenE", width: 390, height: 120 });
session.screens.push({ name: "screenF", width: 1240, height: 650 });

// Converting the JSON string with JSON.stringify()
// then saving with localStorage in the name of session
localStorage.setItem("session", JSON.stringify(session));

// Example of how to transform the String generated through
// JSON.stringify() and saved in localStorage in JSON object again
const restoredSession = JSON.parse(localStorage.getItem("session"));

// Now restoredSession variable contains the object that was saved
// in localStorage
console.log(restoredSession);

Engines implementing the well-formed JSON.stringify specification will stringify lone surrogates (any code point from U+D800 to U+DFFF) using Unicode escape sequences rather than literally (outputting lone surrogates). Before this change, such strings could not be encoded in valid UTF-8 or UTF-16:

JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(1906, 0, 2, 15, 4, 5));
// '"1906-01-02T15:04:05.000Z"'

JSON.stringify({ x: 5, y: 6 });
// '{"x":5,"y":6}'
JSON.stringify([new Number(3), new String("false"), new Boolean(false)]);
// '[3,"false",false]'

// String-keyed array elements are not enumerable and make no sense in JSON
const a = ["foo", "bar"];
a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
JSON.stringify(a);
// '["foo","bar"]'

JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] });
// '{"x":[10,null,null,null]}'

// Standard data structures
JSON.stringify([
  new Set([1]),
  new Map([[1, 2]]),
  new WeakSet([{ a: 1 }]),
  new WeakMap([[{ a: 1 }, 2]]),
]);
// '[{},{},{},{}]'

// TypedArray
JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
// '[{"0":1},{"0":1},{"0":1}]'
JSON.stringify([
  new Uint8Array([1]),
  new Uint8ClampedArray([1]),
  new Uint16Array([1]),
  new Uint32Array([1]),
]);
// '[{"0":1},{"0":1},{"0":1},{"0":1}]'
JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
// '[{"0":1},{"0":1}]'

// toJSON()
JSON.stringify({
  x: 5,
  y: 6,
  toJSON() {
    return this.x + this.y;
  },
});
// '11'

// Symbols:
JSON.stringify({ x: undefined, y: Object, z: Symbol("") });
// '{}'
JSON.stringify({ [Symbol("foo")]: "foo" });
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, [Symbol.for("foo")]);
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

// Non-enumerable properties:
JSON.stringify(
  Object.create(null, {
    x: { value: "x", enumerable: false },
    y: { value: "y", enumerable: true },
  }),
);
// '{"y":"y"}'

// BigInt values throw
JSON.stringify({ x: 2n });
// TypeError: BigInt value can't be serialized in JSON
0

But with this change

JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(1906, 0, 2, 15, 4, 5));
// '"1906-01-02T15:04:05.000Z"'

JSON.stringify({ x: 5, y: 6 });
// '{"x":5,"y":6}'
JSON.stringify([new Number(3), new String("false"), new Boolean(false)]);
// '[3,"false",false]'

// String-keyed array elements are not enumerable and make no sense in JSON
const a = ["foo", "bar"];
a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
JSON.stringify(a);
// '["foo","bar"]'

JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] });
// '{"x":[10,null,null,null]}'

// Standard data structures
JSON.stringify([
  new Set([1]),
  new Map([[1, 2]]),
  new WeakSet([{ a: 1 }]),
  new WeakMap([[{ a: 1 }, 2]]),
]);
// '[{},{},{},{}]'

// TypedArray
JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
// '[{"0":1},{"0":1},{"0":1}]'
JSON.stringify([
  new Uint8Array([1]),
  new Uint8ClampedArray([1]),
  new Uint16Array([1]),
  new Uint32Array([1]),
]);
// '[{"0":1},{"0":1},{"0":1},{"0":1}]'
JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
// '[{"0":1},{"0":1}]'

// toJSON()
JSON.stringify({
  x: 5,
  y: 6,
  toJSON() {
    return this.x + this.y;
  },
});
// '11'

// Symbols:
JSON.stringify({ x: undefined, y: Object, z: Symbol("") });
// '{}'
JSON.stringify({ [Symbol("foo")]: "foo" });
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, [Symbol.for("foo")]);
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

// Non-enumerable properties:
JSON.stringify(
  Object.create(null, {
    x: { value: "x", enumerable: false },
    y: { value: "y", enumerable: true },
  }),
);
// '{"y":"y"}'

// BigInt values throw
JSON.stringify({ x: 2n });
// TypeError: BigInt value can't be serialized in JSON
2 represents lone surrogates using JSON escape sequences that can be encoded in valid UTF-8 or UTF-16:

JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(1906, 0, 2, 15, 4, 5));
// '"1906-01-02T15:04:05.000Z"'

JSON.stringify({ x: 5, y: 6 });
// '{"x":5,"y":6}'
JSON.stringify([new Number(3), new String("false"), new Boolean(false)]);
// '[3,"false",false]'

// String-keyed array elements are not enumerable and make no sense in JSON
const a = ["foo", "bar"];
a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
JSON.stringify(a);
// '["foo","bar"]'

JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] });
// '{"x":[10,null,null,null]}'

// Standard data structures
JSON.stringify([
  new Set([1]),
  new Map([[1, 2]]),
  new WeakSet([{ a: 1 }]),
  new WeakMap([[{ a: 1 }, 2]]),
]);
// '[{},{},{},{}]'

// TypedArray
JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
// '[{"0":1},{"0":1},{"0":1}]'
JSON.stringify([
  new Uint8Array([1]),
  new Uint8ClampedArray([1]),
  new Uint16Array([1]),
  new Uint32Array([1]),
]);
// '[{"0":1},{"0":1},{"0":1},{"0":1}]'
JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
// '[{"0":1},{"0":1}]'

// toJSON()
JSON.stringify({
  x: 5,
  y: 6,
  toJSON() {
    return this.x + this.y;
  },
});
// '11'

// Symbols:
JSON.stringify({ x: undefined, y: Object, z: Symbol("") });
// '{}'
JSON.stringify({ [Symbol("foo")]: "foo" });
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, [Symbol.for("foo")]);
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

// Non-enumerable properties:
JSON.stringify(
  Object.create(null, {
    x: { value: "x", enumerable: false },
    y: { value: "y", enumerable: true },
  }),
);
// '{"y":"y"}'

// BigInt values throw
JSON.stringify({ x: 2n });
// TypeError: BigInt value can't be serialized in JSON
1

This change should be backwards-compatible as long as you pass the result of

JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(1906, 0, 2, 15, 4, 5));
// '"1906-01-02T15:04:05.000Z"'

JSON.stringify({ x: 5, y: 6 });
// '{"x":5,"y":6}'
JSON.stringify([new Number(3), new String("false"), new Boolean(false)]);
// '[3,"false",false]'

// String-keyed array elements are not enumerable and make no sense in JSON
const a = ["foo", "bar"];
a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
JSON.stringify(a);
// '["foo","bar"]'

JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] });
// '{"x":[10,null,null,null]}'

// Standard data structures
JSON.stringify([
  new Set([1]),
  new Map([[1, 2]]),
  new WeakSet([{ a: 1 }]),
  new WeakMap([[{ a: 1 }, 2]]),
]);
// '[{},{},{},{}]'

// TypedArray
JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
// '[{"0":1},{"0":1},{"0":1}]'
JSON.stringify([
  new Uint8Array([1]),
  new Uint8ClampedArray([1]),
  new Uint16Array([1]),
  new Uint32Array([1]),
]);
// '[{"0":1},{"0":1},{"0":1},{"0":1}]'
JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
// '[{"0":1},{"0":1}]'

// toJSON()
JSON.stringify({
  x: 5,
  y: 6,
  toJSON() {
    return this.x + this.y;
  },
});
// '11'

// Symbols:
JSON.stringify({ x: undefined, y: Object, z: Symbol("") });
// '{}'
JSON.stringify({ [Symbol("foo")]: "foo" });
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, [Symbol.for("foo")]);
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

// Non-enumerable properties:
JSON.stringify(
  Object.create(null, {
    x: { value: "x", enumerable: false },
    y: { value: "y", enumerable: true },
  }),
);
// '{"y":"y"}'

// BigInt values throw
JSON.stringify({ x: 2n });
// TypeError: BigInt value can't be serialized in JSON
2 to APIs such as
JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(1906, 0, 2, 15, 4, 5));
// '"1906-01-02T15:04:05.000Z"'

JSON.stringify({ x: 5, y: 6 });
// '{"x":5,"y":6}'
JSON.stringify([new Number(3), new String("false"), new Boolean(false)]);
// '[3,"false",false]'

// String-keyed array elements are not enumerable and make no sense in JSON
const a = ["foo", "bar"];
a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
JSON.stringify(a);
// '["foo","bar"]'

JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] });
// '{"x":[10,null,null,null]}'

// Standard data structures
JSON.stringify([
  new Set([1]),
  new Map([[1, 2]]),
  new WeakSet([{ a: 1 }]),
  new WeakMap([[{ a: 1 }, 2]]),
]);
// '[{},{},{},{}]'

// TypedArray
JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
// '[{"0":1},{"0":1},{"0":1}]'
JSON.stringify([
  new Uint8Array([1]),
  new Uint8ClampedArray([1]),
  new Uint16Array([1]),
  new Uint32Array([1]),
]);
// '[{"0":1},{"0":1},{"0":1},{"0":1}]'
JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
// '[{"0":1},{"0":1}]'

// toJSON()
JSON.stringify({
  x: 5,
  y: 6,
  toJSON() {
    return this.x + this.y;
  },
});
// '11'

// Symbols:
JSON.stringify({ x: undefined, y: Object, z: Symbol("") });
// '{}'
JSON.stringify({ [Symbol("foo")]: "foo" });
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, [Symbol.for("foo")]);
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

// Non-enumerable properties:
JSON.stringify(
  Object.create(null, {
    x: { value: "x", enumerable: false },
    y: { value: "y", enumerable: true },
  }),
);
// '{"y":"y"}'

// BigInt values throw
JSON.stringify({ x: 2n });
// TypeError: BigInt value can't be serialized in JSON
02 that will accept any valid JSON text, because they will treat Unicode escapes of lone surrogates as identical to the lone surrogates themselves. Only if you are directly interpreting the result of
JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(1906, 0, 2, 15, 4, 5));
// '"1906-01-02T15:04:05.000Z"'

JSON.stringify({ x: 5, y: 6 });
// '{"x":5,"y":6}'
JSON.stringify([new Number(3), new String("false"), new Boolean(false)]);
// '[3,"false",false]'

// String-keyed array elements are not enumerable and make no sense in JSON
const a = ["foo", "bar"];
a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
JSON.stringify(a);
// '["foo","bar"]'

JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] });
// '{"x":[10,null,null,null]}'

// Standard data structures
JSON.stringify([
  new Set([1]),
  new Map([[1, 2]]),
  new WeakSet([{ a: 1 }]),
  new WeakMap([[{ a: 1 }, 2]]),
]);
// '[{},{},{},{}]'

// TypedArray
JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
// '[{"0":1},{"0":1},{"0":1}]'
JSON.stringify([
  new Uint8Array([1]),
  new Uint8ClampedArray([1]),
  new Uint16Array([1]),
  new Uint32Array([1]),
]);
// '[{"0":1},{"0":1},{"0":1},{"0":1}]'
JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
// '[{"0":1},{"0":1}]'

// toJSON()
JSON.stringify({
  x: 5,
  y: 6,
  toJSON() {
    return this.x + this.y;
  },
});
// '11'

// Symbols:
JSON.stringify({ x: undefined, y: Object, z: Symbol("") });
// '{}'
JSON.stringify({ [Symbol("foo")]: "foo" });
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, [Symbol.for("foo")]);
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

// Non-enumerable properties:
JSON.stringify(
  Object.create(null, {
    x: { value: "x", enumerable: false },
    y: { value: "y", enumerable: true },
  }),
);
// '{"y":"y"}'

// BigInt values throw
JSON.stringify({ x: 2n });
// TypeError: BigInt value can't be serialized in JSON
2 do you need to carefully handle
JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'

JSON.stringify(new Date(1906, 0, 2, 15, 4, 5));
// '"1906-01-02T15:04:05.000Z"'

JSON.stringify({ x: 5, y: 6 });
// '{"x":5,"y":6}'
JSON.stringify([new Number(3), new String("false"), new Boolean(false)]);
// '[3,"false",false]'

// String-keyed array elements are not enumerable and make no sense in JSON
const a = ["foo", "bar"];
a["baz"] = "quux"; // a: [ 0: 'foo', 1: 'bar', baz: 'quux' ]
JSON.stringify(a);
// '["foo","bar"]'

JSON.stringify({ x: [10, undefined, function () {}, Symbol("")] });
// '{"x":[10,null,null,null]}'

// Standard data structures
JSON.stringify([
  new Set([1]),
  new Map([[1, 2]]),
  new WeakSet([{ a: 1 }]),
  new WeakMap([[{ a: 1 }, 2]]),
]);
// '[{},{},{},{}]'

// TypedArray
JSON.stringify([new Int8Array([1]), new Int16Array([1]), new Int32Array([1])]);
// '[{"0":1},{"0":1},{"0":1}]'
JSON.stringify([
  new Uint8Array([1]),
  new Uint8ClampedArray([1]),
  new Uint16Array([1]),
  new Uint32Array([1]),
]);
// '[{"0":1},{"0":1},{"0":1},{"0":1}]'
JSON.stringify([new Float32Array([1]), new Float64Array([1])]);
// '[{"0":1},{"0":1}]'

// toJSON()
JSON.stringify({
  x: 5,
  y: 6,
  toJSON() {
    return this.x + this.y;
  },
});
// '11'

// Symbols:
JSON.stringify({ x: undefined, y: Object, z: Symbol("") });
// '{}'
JSON.stringify({ [Symbol("foo")]: "foo" });
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, [Symbol.for("foo")]);
// '{}'
JSON.stringify({ [Symbol.for("foo")]: "foo" }, (k, v) => {
  if (typeof k === "symbol") {
    return "a symbol";
  }
});
// undefined

// Non-enumerable properties:
JSON.stringify(
  Object.create(null, {
    x: { value: "x", enumerable: false },
    y: { value: "y", enumerable: true },
  }),
);
// '{"y":"y"}'

// BigInt values throw
JSON.stringify({ x: 2n });
// TypeError: BigInt value can't be serialized in JSON
2's two possible encodings of these code points.