0

I just simply to use JSON.stringify({"a": "123"}) to stringify a json

In chrome, the " semicolon will not be escaped, it will echo

JSON.stringify({"a": "123"})
"{"a":"123"}"

But if I use the same code in safari, the " semicolon will be escaped, like this

JSON.stringify({"a": "123"})
"{\"a\":\"123\"}"

I want to know the reason about why the chrome and safari have the different result

2

1 Answer 1

5

It's not about JSON.stringify, it's about how the console displays value literals.

Safari chooses to make the entire line a valid literal. I.e. you could copy-paste the entire line into Javascript source code, and it'd be valid.

Safari displays "foo\"bar"

Chrome opts to just add decorative "" marks around the line to signify that it's a string value, but displays only the string contents as-is, without making it into a valid literal.

Chrome displays "foo"bar"

The advantage of Safari's method is that you can copy-paste values as code, while Chrome's advantage is that you can read a string's contents without needing to mentally parse it according to string escaping rules.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.