---
title: "Discussion on \"Pretty print using JSON.stringify()\" | Hashnode"
description: "Discussion on \"Pretty print using JSON.stringify()\". Syntax: JSON.stringify(value[, replacer[, space]])\nwhere, replacer is used to apply modifications to the value while stringifying\nand space specifies the indentation of the value.\nconst obj = {\n  name: \"ABC\",\n  age: 200,\n  skills: [\"C\", \"JavaScript\",..."
url: "https://hashnode.com/posts/pretty-print-using-jsonstringify/5fe21f8bc65ad56bef3a2f96"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Pretty print using JSON.stringify()](https://codedrops.hashnode.dev/pretty-print-using-jsonstringify?utm_source=hashnode&utm_medium=feed)

Syntax: JSON.stringify(value\[, replacer\[, space\]\]) where, replacer is used to apply modifications to the value while stringifying and space specifies the indentation of the value. const obj = { name: "ABC", age: 200, skills: \["C", "JavaScript",...

Dec 22, 2020 · 1 min read · codedrops.hashnode.dev  
Read the article

-   [#javascript](https://hashnode.com/tag/javascript)
-   [#codenewbies](https://hashnode.com/tag/codenewbies)
-   [#web-development](https://hashnode.com/tag/web-development)
-   [#webdev](https://hashnode.com/tag/webdev)

## Written by

[Mehul Lakhanpal](https://hashnode.com/@ml318097)

@ml318097 · Bangalore, India

  
[View profile](https://hashnode.com/@ml318097)

Full-stack Developer

## Responses 1

Markdown supported · ⌘↵ to post

[Purvi Barot](https://hashnode.com/@purvi)

[Jan 11, 2021](https://hashnode.com/posts/pretty-print-using-jsonstringify/5fe21f8bc65ad56bef3a2f96/comment/5ffbde41a538d00135156397)

The JSON.stringify() method converts a JavaScript object or 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.

```
console.log(JSON.stringify({ x: 5, y: 6 }));
// expected output: "{"x":5,"y":6}"

console.log(JSON.stringify([new Number(3), new String('false'), new Boolean(false)]));
// expected output: "[3,"false",false]"

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

console.log(JSON.stringify(new Date(2006, 0, 2, 15, 4, 5)));
// expected output: ""2006-01-02T15:04:05.000Z""
```

**Examples using JSON.stringify:**

```
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(2006, 0, 2, 15, 4, 5))
// '"2006-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
let 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' }, function(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
```
