Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
53
rated 0 times [  57] [ 4]  / answers: 1 / hits: 32465  / 11 Years ago, mon, july 8, 2013, 12:00:00

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.


More From » json

 Answers
63

There is no way to pass third parameter without passing second parameter in JavaScript.

So null is a placeholder for replacer function when you need to pass space.



https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify


[#77125] Sunday, July 7, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
harveys

Total Points: 113
Total Questions: 88
Total Answers: 79

Location: Oman
Member since Fri, Dec 23, 2022
1 Year ago
;