JSON.stringify Basics
You'll learn how to convert JavaScript values into JSON-formatted strings so you can store or transmit data.
What JSON.stringify Does
JSON.stringify() is a built-in JavaScript method that takes a value (usually an object or array) and converts it into a JSON string. Think of it like a translator that turns your JavaScript data into a universal text format that can be saved to files, sent across networks, or stored in databases.
When you stringify a value, JavaScript inspects it and creates a text representation following JSON syntax rules (which you just learned: double quotes for strings, no trailing commas, limited types, etc.).
Basic Usage
const user = {
name: "Alice",
age: 30,
active: true
};
const jsonString = JSON.stringify(user);
console.log(jsonString);
// Output: '{"name":"Alice","age":30,"active":true}'
Notice the result is a string, not an object. You can now save this text anywhere.
What Gets Serialized
JSON.stringify converts most common types:
- Objects and arrays
- Strings, numbers, booleans, and
null
But certain values are skipped or converted:
undefinedvalues are omitted from objects- Functions are omitted
- Symbol properties are omitted
NaNandInfinitybecomenull
const data = {
valid: 42,
missing: undefined,
doSomething: function() {}
};
console.log(JSON.stringify(data));
// Output: '{"valid":42}'
Key Takeaway: JSON.stringify() converts JavaScript values into JSON strings for storage or transmission, but only serializes JSON-compatible types—undefined, functions, and symbols are automatically excluded.