JavaScript Objects, and Why You See [object Object]
A JavaScript object is a set of named values written as { key: value }. You read a value with obj.key or obj['key'], list keys with Object.keys and pairs with Object.entries. The text [object Object] appears when an object is turned into a string; use JSON.stringify(obj) to print its contents.
| Task | Code |
|---|---|
| Create | const user = { name: "Ana", age: 31 } |
| Read a property | user.name or user["name"] |
| Read with a variable key | user[key] |
| Safe nested read | user.address?.city |
| List keys / values / pairs | Object.keys(user) / Object.values(user) / Object.entries(user) |
| Shallow copy and merge | { ...user, age: 32 } |
| Deep copy | structuredClone(user) |
| Print the contents | JSON.stringify(user, null, 2) |
Updated
Objects are the shape almost every API response, config file and React prop arrives in. This guide covers the handful of operations you use daily, then deals with the two classic surprises: [object Object] showing up in the page or an alert, and a copy that secretly shares its nested data with the original.
{
"name": "Ana",
"age": 31,
"address": { "city": "Lisbon", "zip": "1100" },
"tags": ["admin", "editor"]
}Showing the real value instead of [object Object]
Four ways to display an object
Only the first line produces [object Object]. The others print readable contents.
const user = { name: "Ana", tags: ["admin"] };
el.textContent = "User: " + user; // User: [object Object]
el.textContent = "User: " + JSON.stringify(user); // User: {"name":"Ana","tags":["admin"]}
console.log("User:", user); // expandable object in DevTools
console.table(Object.entries(user)); // one row per keyPretty print inside the page
A pre element keeps the line breaks and indentation that JSON.stringify adds with its third argument.
<pre>{
"name": "Ana",
"address": {
"city": "Lisbon"
}
}</pre>Transforming objects with entries and fromEntries
Add tax to every price
entries turns the object into pairs, map changes each value, fromEntries builds a new object.
const prices = { tea: 3, cake: 4.5, juice: 4 };
const withTax = Object.fromEntries(
Object.entries(prices).map(([item, p]) => [item, +(p * 1.2).toFixed(2)])
);
// { tea: 3.6, cake: 5.4, juice: 4.8 }Spread versus structuredClone
The spread copy shares the inner address object; the clone does not.
const original = { name: "Ana", address: { city: "Lisbon" } };
const shallow = { ...original };
const deep = structuredClone(original);
shallow.address.city = "Porto";
console.log(original.address.city); // "Porto" (shared)
deep.address.city = "Faro";
console.log(original.address.city); // still "Porto"
Dot notation versus brackets
Dot notation is shorter and works when the key is a valid identifier you know while writing the code. Brackets are required when the key lives in a variable, contains a space or a dash, or starts with a digit, for example headers['content-type'].
Reading a key that does not exist gives undefined rather than an error. Reading a property of that undefined is what throws, which is why optional chaining (user.address?.city) is so handy on API data.
Shallow and deep copies
Spread copies only the top level. The new object gets its own slots, but if a slot holds another object, both copies point at that same inner object, so editing copy.address.city also edits the original.
structuredClone walks the whole structure and builds new inner objects too. It does not keep class prototypes, getters or setters, so a class instance comes back as a plain object with the same data.
How it works
- Paste or edit JSON in the inspector. It is parsed in your browser with JSON.parse.
- Each row shows a common way of turning the object into text or a list, including the one that produces [object Object].
- Use the copy experiment to change a nested value on a spread copy and on a structuredClone copy, and see which one leaks back into the original.
- The code examples below are ready to paste into your editor or the browser console.
Tips
- To see an object in the console, log it on its own with console.log(obj) or console.table(obj). Joining it to a string with + flattens it to [object Object].
- Object.keys returns keys as strings, even for numeric-looking keys, and integer-like keys come out in ascending order before the rest.
- JSON.stringify skips properties whose value is undefined, a function or a symbol, and throws a TypeError on circular references.
- Use Object.hasOwn(obj, key) to test whether the object itself has a key; the in operator also looks at inherited properties.
Frequently asked questions
Why does my code print [object Object]?
Something converted the object to a string: a + with text, a template literal, an alert, element.textContent or innerText. The default toString for plain objects returns the fixed text [object Object]. Print JSON.stringify(obj) instead, or pick the property you meant, such as obj.name.
How do I loop over an object in JavaScript?
Use for (const [key, value] of Object.entries(obj)) { ... }. A plain object is not iterable, so for...of on it directly throws a TypeError. for...in also works but includes inherited enumerable keys.
How do I copy an object without linking it to the original?
For a flat object, { ...obj } or Object.assign({}, obj) is enough. If the object holds nested objects or arrays, use structuredClone(obj), which copies every level. It handles Dates, Maps and Sets but throws a DataCloneError on functions and DOM nodes.
How do I turn an object into an array and back?
Object.entries(obj) gives an array of [key, value] pairs. Transform it with map or filter, then rebuild an object with Object.fromEntries(pairs). This is the usual way to map over an object's values.
What is the difference between an object and a Map?
A Map accepts any type as a key, remembers insertion order for all keys and has a size property. Plain objects use string or symbol keys and are what JSON produces. Note that JSON.stringify turns a Map into {}, so convert it first with Object.fromEntries(map).
Related: JavaScript loops compared, JavaScript array methods, JSON formatter, Save objects in localStorage, TypeScript interfaces.
Build your site free or see all free tools. On We.Inc you can build your site free, with no card and no trial needed to start. AI building is on paid plans from $20 a month.
More in Guides
- Reload the Page with JavaScript: location.reload()
- Scroll to an Element with JavaScript
- Sort an Array of Objects in JavaScript
- JavaScript String Methods Cheat Sheet (Live)
- JavaScript Websites and AI Crawlers: Can They See Your Pages? | How-to Guide
- Build a Jewelry Website: Pages, Costs and a Working Example | How-to Guide
- Jira MCP Server: Setup, Tools and Admin Controls
- Keyboard and Screen Reader Testing, Step by Step
Product
Who It's For
- For Agencies
- For Freelancers
- For Hosting Providers
- For SaaS Platforms
- For Startups
- For Financial Services
- For Healthcare
Features
Resources
- Blog
- Help Center
- Free Business Tools
- Free UI Components
- Website Templates
- Template Collections
- Website Examples
- Guides
- Comparisons
- Alternatives
- Best Lists
- Reviews
- Pricing Guides
- Use Cases
- Glossary & Guides
- Integrations
- Developers
- Benchmark
- Windows App