In JavaScript's JSON.stringify()
function, I occasionally see the following syntax:
JSON.stringify(obj, null, 4)
However, I can't get what the second argument, null
, is supposed to do. As long as I know, the above function takes an object as its first argument, and converts it to a string variable. The third argument, 4
in this case, indents and pretty-prints the resultant string object. But I can't see what the second argument tries to do even after I read the explanation on the official document... So what does the argument do? Or is it just there in order to take in the third argument? (But I think then the function should take both argument name and its parameter, such that for example, JSON.stringify(obj, space=4)
. I'm not sure whether that sort of syntax is allowed in JavaScript, so forgive me if it's not. But I don't know my expectation is correct in the first place, so would like to throw a question anyway).
Thanks.