Reputation: 2235
demo.js:
let obj = { foo: "bar\"bar" }
console.log(JSON.stringify(obj)) // it will get: {"foo":"bar\"bar"}
console.log(999, JSON.stringify(obj)) // it will get: 999 '{"foo":"bar\\"bar"}'
and then run:
node demo.js
why the two lines get different results?
Upvotes: 0
Views: 89
Reputation: 151
The answer can be found if you look at this documentation. All the arguments are passed to util.format()
, which returns a concatenation of all the given arguments if the first argument is not a string.
It first converts each argument to a string representation using util.inspect()
before concatenating them. This explains why both lines return different results.
Upvotes: 1