JavaScript (ES2026) reference · JSON and Global Functions

JavaScript JSON.stringify() Method

Convert a JavaScript value to a JSON string, optionally filtering keys and pretty-printing with indentation.

JavaScript (ES2026) Since ES5

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

JSON.stringify(value, replacer, space)

Parameters

ParameterDescription
value The value to convert.
replacer optional A function (key, value) or an array of keys to include.
space optional Indentation: a number of spaces or a string.

Return value

A JSON string, or undefined for values like functions.

Examples

Compact and prettyTry it yourself
const order = { id: 7, items: ["tea", "cake"], paid: true };
console.log(JSON.stringify(order));
console.log(JSON.stringify(order, null, 2));

Output

{"id":7,"items":["tea","cake"],"paid":true}
{
  "id": 7,
  "items": [
    "tea",
    "cake"
  ],
  "paid": true
}
Leave out keysTry it yourself
const user = { name: "Ada", password: "secret", age: 36 };
console.log(JSON.stringify(user, ["name", "age"]));
console.log(JSON.stringify(user, (k, v) => (k === "password" ? undefined : v)));

Output

{"name":"Ada","age":36}
{"name":"Ada","age":36}
What happens to special valuesTry it yourself
console.log(JSON.stringify({ when: new Date(0), fn() {}, missing: undefined, nan: NaN, big: 10 }));

Output

{"when":"1970-01-01T00:00:00.000Z","nan":null,"big":10}

Common mistakes

  • undefined, functions and symbols are skipped in objects (and become null in arrays).
  • NaN and Infinity become null; Date objects become ISO strings.
  • Circular references throw a TypeError; BigInt values throw unless you convert them.

Version history

VersionChange
JavaScript ES5Added

Was this page helpful?