MJGwater MJGwater - 1 month ago 5x
Javascript Question

behavior of third argument for JSON.stringify

I'm confused why there are 10 dashes before the numbers 1,2,3 in the output of the code below. (in repl here: https://repl.it/DRuH)

var a = {
b: 42,
c: "42",
d: [1,2,3]

console.log(JSON.stringify( a, null, "-----" ));

The output is

-----"b": 42,
-----"c": "42",
-----"d": [

Why wouldn't [1,2,3] appear together just as b and c still look the same?

Why are there 10 dashes instead of 5 dashes for these numbers?


When you give the space argument, each element of an array or object is placed on its own line, and indented to indicate its depth in the hierarchy of the objects and arrays. Since the numbers in the d array are the 2nd level of the hierarchy, they get 2 copies of the ----- string.