JSON Stringify Output
Predict Output Easy
Question
What does this code print?
Code
let str = JSON.stringify({a: 1, b: 2});
console.log(str);
Hint
JSON.stringify converts an object to a JSON string.
Explanation
JSON.stringify converts the object {a: 1, b: 2} to the JSON string '{"a":1,"b":2}'. Note that property names are double-quoted, and there are no spaces by default. You can add formatting with JSON.stringify(obj, null, 2) for pretty-print with 2-space indentation.
Related Resources
Related Lessons
- JSON
Learn JSON data format.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.