JavaScript Objects
An object groups related data and behaviour under one name. Where a variable holds one value, an object holds many named values called properties. Real-world things map naturally onto objects: a user has a name and an email, a product has a title and a price.
Object literals
The easiest way to create an object is an object literal: curly braces containing key: value pairs separated by commas.
const book = {
title: "The Pragmatic Programmer",
year: 1999,
authors: ["Andrew Hunt", "David Thomas"],
available: true,
};
console.log(book);
console.log(book.title);
console.log(book.authors[1]);{
title: 'The Pragmatic Programmer',
year: 1999,
authors: [ 'Andrew Hunt', 'David Thomas' ],
available: true
}
The Pragmatic Programmer
David Thomas
Keys are strings (quotes are optional when the key is a valid identifier). Values can be anything — numbers, strings, arrays, other objects, functions.
Reading and changing properties
Use dot notation (obj.key) for fixed names and bracket notation (obj["key"]) when the name is in a variable or isn't a valid identifier:
const car = { brand: "Volvo", "top speed": 180 };
console.log(car.brand);
console.log(car["top speed"]);
const field = "brand";
console.log(car[field]);
car.color = "silver"; // add a property
car.brand = "Saab"; // change a property
delete car["top speed"]; // remove a property
console.log(car);
console.log(car.wheels); // missing -> undefinedVolvo
180
Volvo
{ brand: 'Saab', color: 'silver' }
undefined
Methods
A property whose value is a function is called a method. Inside a method, this refers to the object the method was called on:
const counter = {
count: 0,
increment() {
this.count++;
return this.count;
},
describe() {
return `Counter is at ${this.count}`;
},
};
counter.increment();
counter.increment();
console.log(counter.describe());Counter is at 2
increment() { … } is shorthand for increment: function () { … }.
Shorthand properties
When a property name matches a variable name, you can write it just once:
const name = "Ravi";
const role = "admin";
const user = { name, role, active: true };
console.log(user);{ name: 'Ravi', role: 'admin', active: true }
Objects are references
Assigning an object to another variable doesn't copy it — both variables refer to the same object. Comparing objects with === checks whether they're the same object, not whether they look alike:
const original = { score: 10 };
const alias = original;
alias.score = 99;
console.log(original.score);
const copy = { ...original }; // spread creates a shallow copy
copy.score = 1;
console.log(original.score, copy.score);
console.log({ a: 1 } === { a: 1 });
console.log(alias === original);99
99 1
false
true
Nested objects
Objects can contain other objects, forming a tree of data. Optional chaining ?. safely reads deep properties that may be missing:
const order = {
id: 1042,
customer: { name: "Lena", address: { city: "Oslo" } },
};
console.log(order.customer.address.city);
console.log(order.shipping?.method); // no error, just undefinedOslo
undefined
Tip: Objects aren't just for data — almost everything in JavaScript is an object: arrays, functions, dates, regular expressions, DOM elements. Learning objects well pays off everywhere.
Test yourself
How do you read a property whose name is stored in the variable key?
obj.keyobj[key]obj->keyobj(key)
What is { x: 1 } === { x: 1 }?
- true
- false
- undefined
- An error
What is a method?
- A property whose value is a function
- A property that is read-only
- Any object with a name
- A string key