Objects
An object groups related information about one thing — a user, a product, an order — using named properties.
On this page · 5 min · Beginner
On this page
- Learning objectives
- Explanation
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Beginner
- Updated
- Oct 27, 2026
Learning objectives#
- Create objects with properties
- Read and change properties with dot and bracket notation
- List keys with Object.keys
- Understand that objects are copied by reference
Explanation#
An object is a set of key: value pairs inside curly braces. Values can be anything, including arrays and other objects.
Read with dot notation (user.name) or brackets (user['name']). Brackets are needed when the key is in a variable or has spaces.
You can add properties at any time (user.city = 'Pune') and remove them with delete.
Assigning an object to another variable does NOT copy it — both names point to the same object. Use the spread syntax { ...obj } for a shallow copy.
Code example: A user profile#
const user = { name: 'Asha', age: 22, skills: ['JS', 'CSS'] };
console.log(user.name, user['age']);
user.city = 'Pune';
delete user.age;
console.log(Object.keys(user));
const sameUser = user; // NOT a copy — same object
sameUser.name = 'Meera';
console.log(user.name);
const clone = { ...user, name: 'Ravi' }; // a real (shallow) copy
console.log(user.name, clone.name);
Asha 22 [ 'name', 'skills', 'city' ] Meera Meera Ravi
Step-by-step explanation#
- Dot and bracket notation read the same kind of property.
- city is added and age is deleted, so the keys are now name, skills, city.
- sameUser points at the same object, so changing it changes user too.
- The spread copy is independent: changing clone.name does not affect user.
Real-world use case#
API responses arrive as objects: { id, title, price, category }. Your UI reads their properties to display a product card.
Common mistakes#
const b = a creates a second name for the same object. Mutating b mutates a.
user.address.city throws if address is undefined. Use optional chaining: user.address?.city.
Best practices#
- Use dot notation by default, brackets for dynamic keys
- Copy with { ...obj } before changing shared data
- Use structuredClone(obj) for deep copies
- Keep object shapes consistent
Remember this#
Objects are labelled boxes: key → value.
= shares the same box. { ...obj } makes a new box.
Key in a variable? Use brackets: obj[key].
Practice exercise#
Your task: Create a product object with name, price and inStock. Increase the price by 10%, then print all keys and the new price.
Show solution
const product = { name: 'Pen', price: 20, inStock: true };
product.price = product.price * 1.1;
console.log(Object.keys(product), product.price.toFixed(2)); // [ 'name', 'price', 'inStock' ] 22.00
Mini quiz#
What is printed?
const a = { n: 1 };
const b = a;
b.n = 2;
console.log(a.n);
Want more? Practise all JavaScript MCQs · JavaScript interview questions
My notes#
Saved in this browser.