The most common way to create an object is an object literal: a pair of curly braces containing key: value pairs, separated by commas.
In real life, a car is an object. Every car has the same kinds of properties (name, model, weight, color) and the same methods (start, drive, brake, stop). The property values differ from car to car, and the methods are performed at different times:
| Properties | Methods |
|---|---|
| car.name = Fiat | car.start() |
| car.model = 500 | car.drive() |
| car.weight = 850kg | car.brake() |
| car.color = white | car.stop() |
JavaScript objects work the same way: properties are named values stored as key: value pairs, and methods are functions stored the same way.
const book = {
title: "Dune",
author: "Frank Herbert",
pages: 412,
available: true
};
{ } start and end the objectconsole.log(book.title); // "Dune"
console.log(book["pages"]); // 412
console.log(book.price); // undefined (no such property)
Dot notation is the usual choice. Bracket notation is covered fully in the next lesson.
A property can hold any type: strings, numbers, booleans, arrays, other objects, even functions:
const player = {
name: "Kai",
level: 7,
online: false,
inventory: ["sword", "potion", "map"],
position: { x: 120, y: 45 },
greet() { return "Ready!"; }
};
console.log(player.inventory[1]); // "potion"
console.log(player.position.x); // 120
console.log(player.greet()); // "Ready!"
Chaining dots and brackets like player.position.x lets you reach into objects inside objects.
You can create an empty object and fill it in as you go:
const settings = {};
settings.theme = "dark";
settings.fontSize = 18;
console.log(settings);
Recall from the Const lesson: const locks which object the variable points to, not the object's contents.
const user = { name: "Ada" };
user.name = "Grace"; // fine: changing a property
user.age = 36; // fine: adding a property
user = { name: "Alan" }; // TypeError: cannot point user at a new object
Keys follow the same rules as variable names. If a key needs a space, a hyphen, or starts with a digit, wrap it in quotes:
const item = {
name: "Lamp",
"shipping-cost": 4.99,
"2day": true
};
console.log(item["shipping-cost"]); // 4.99 (must use brackets)
shippingCost) whenever you control the names. Quoted keys usually only appear when data comes from somewhere else.
Two objects with identical contents are still two different objects:
const a = { x: 1 };
const b = { x: 1 };
const c = a;
console.log(a === b); // false (different objects)
console.log(a === c); // true (same object, two names)
c.x = 99;
console.log(a.x); // 99 (a and c are the same object)
const c = a does not copy the object. It gives the same object a second name. To make an independent copy, use const c = { ...a };
// Array: an ordered list of similar things, accessed by position
const scores = [88, 92, 75];
// Object: one thing with named details, accessed by key
const test = { subject: "Math", score: 88, date: "2026-10-02" };
// Often combined: a list of objects
const tests = [
{ subject: "Math", score: 88 },
{ subject: "Science", score: 92 }
];
console.log(tests[1].subject); // "Science"
You may see objects created with the new keyword:
const person = new Object({ firstName: "John", age: 50 });
It works, but there is no reason to use it. The object literal { } does the same thing and is shorter, clearer, and faster. Declare objects with const and a literal.
If you understand objects, you understand JavaScript, because almost everything in it is an object: arrays, dates, functions, maps, sets, regular expressions, errors, and Math. All values except primitives are objects. There are 7 primitive types, each holding a single simple value:
| string | "Hello" | bigint | 12345678901234n |
| number | 3.14 | null | null |
| boolean | true | undefined | undefined |
| symbol | Symbol("id") |
Together with Object, that makes the 8 data types from JS Types.
What does this log?
const pet = { name: "Rex", toys: ["ball", "rope"] };
console.log(pet.toys[1]);
rope. pet.toys is the array, and [1] picks its second item.What does this log?
const a = { n: 1 };
const b = a;
b.n = 5;
console.log(a.n);
5. const b = a does not copy the object; it gives the same object a second name. Changing it through b changes it for a too.Which line causes an error?
const u = {}; u.name = "Ada";const u = { name: "Ada" }; u.name = "Grace";const u = { name: "Ada" }; u = { name: "Grace" };const u = { "first name": "Ada" };const stops the variable being pointed at a different object: TypeError: Assignment to constant variable. Adding or changing properties (A, B) is fine, and D is a valid quoted key.movie object with a title, year, rating, and an array of actors===, and explain the result