Introduction to objects
An object in JavaScript is a collection of named values called properties. Each property has a key, usually a string, and a value, which can be anything: a number, a string, an array, another object or a function. Objects let you group related data and behaviour together, such as everything about a user, a product or a setting, instead of juggling many loose variables.
Creating an object literal
The most common way to create an object is with curly braces. Properties are written as key: value pairs separated by commas.
const book = {
title: 'Web Basics Handbook',
pages: 280,
inStock: true,
tags: ['programming', 'beginner'],
author: { name: 'A. Writer', country: 'India' },
summary() {
return `${this.title} (${this.pages} pages)`;
},
};
console.log(book.title); // dot notation
console.log(book['pages']); // bracket notation
console.log(book.author.name); // nested property
console.log(book.summary()); // calling a method
A function stored as a property is called a method. The shorthand summary() { ... } is the modern way to write one, and inside it this refers to the object.
Dot notation versus bracket notation
Dot notation is shorter and used most of the time. Bracket notation is needed when the key is stored in a variable, contains spaces or hyphens, or starts with a number.
For example, if const field = 'pages', then book[field] reads the pages property, whereas book.field would look for a property literally called "field". A header object with the key 'Content-Type' must be read as headers['Content-Type'], because the hyphen would be treated as a minus sign in dot notation.
Adding, changing and removing properties
Objects are flexible: you can add a property simply by assigning it, change one by assigning again, and remove one with delete. Reading a property that does not exist gives undefined rather than an error.
book.price = 450; // add
book.pages = 300; // update
delete book.inStock; // remove
'price' in book; // true
book.discount; // undefined
book.author?.email; // undefined, safe even if author were missing
The optional chaining operator ?. stops and returns undefined instead of throwing when something in the middle of the path is missing.
Useful shortcuts
- Shorthand properties:
{ name, age }is the same as{ name: name, age: age }. - Destructuring:
const { title, pages } = book;copies properties into variables. - Spread:
const copy = { ...book, price: 400 };makes a shallow copy with one change. - Looping:
Object.keys,Object.valuesandObject.entriesreturn arrays you can loop over.
The const and copying confusion
Many beginners expect const to freeze an object. It does not. const only stops you reassigning the variable to a different object; you can still change the properties. To prevent changes, use Object.freeze(), which is shallow.
A related surprise is that objects are stored by reference. Writing const other = book does not copy anything; both names point to the same object, so changing other.title also changes book.title. Use spread for a shallow copy, or structuredClone(book) for a deep copy that also duplicates nested objects. Methods cannot be cloned this way, so structuredClone suits plain data.
Comparing objects with === also checks the reference, not the contents. Two separate objects with identical properties are not equal.
Finally, the order of keys is mostly predictable: string keys keep insertion order, but keys that look like whole numbers are listed first in ascending order. Do not rely on key order for logic; use an array when order matters.
Main points
Objects group named properties and methods. Use dot notation by default and brackets for dynamic or unusual keys. Remember that const does not freeze an object, and that assigning an object to a new variable shares it rather than copying it.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.