Group Related Values With Names
JavaScript Objects
An object is a collection of named values. It's how JavaScript models everything from users to events to API responses.
What you'll learn
- Create objects with literal syntax
- Read properties with dot and bracket notation
- Mutate, add, and delete properties
An object is a collection of named values. Each name is called a key (or property), and each name maps to a value.
const user = {
name: "Ada",
age: 36,
isAdmin: true,
};
console.log(user); Objects model things: a user, a song, a button, an HTTP response. Anything with multiple named pieces of data.
Reading Properties
Two ways:
const user = { name: "Ada", age: 36 };
console.log(user.name); // "Ada"
console.log(user["name"]); // "Ada"
console.log(user.age); // 36
console.log(user["age"]); // 36 - Dot notation (
user.name) — concise, works for any identifier-shaped key. - Bracket notation (
user["name"]) — required when the key has spaces, hyphens, or comes from a variable.
const user = {
"first name": "Ada", // key with a space
};
// console.log(user.first name); // SyntaxError
console.log(user["first name"]); // "Ada"
const key = "age";
const u = { age: 36 };
console.log(u[key]); // 36 ← compute the key Missing Properties Return undefined
Asking for a key that doesn’t exist doesn’t crash — you just get
undefined.
const user = { name: "Ada" };
console.log(user.email); // undefined
console.log(user["email"]); // undefined Adding and Updating Properties
Assignment adds a property if it doesn’t exist, or updates it if it
does. (Yes — works through a const. const makes the binding
constant.)
const user = { name: "Ada" };
user.age = 36; // add a new property
user.name = "Ada Lovelace"; // update an existing one
console.log(user); // { name: 'Ada Lovelace', age: 36 } Deleting a Property
const user = { name: "Ada", age: 36, email: "a@b.c" };
delete user.email;
console.log(user); // { name: 'Ada', age: 36 }
console.log(user.email); // undefined delete is rarely needed — setting a property to undefined or
null is usually more predictable. We don’t really delete from
objects; we build new ones.
Checking Whether a Property Exists
const user = { name: "Ada", age: undefined };
console.log("name" in user); // true
console.log("email" in user); // false
console.log("age" in user); // true ← key exists, value undefined
console.log(user.age === undefined); // also true — different question
console.log(Object.hasOwn(user, "name")); // true (modern, recommended) Object.hasOwn(obj, key) is the modern way to ask “does this object
have this OWN property?” — it ignores inherited ones.
Nested Objects
Values can be other objects, building up a tree.
const user = {
name: "Ada",
address: {
city: "London",
country: "UK",
},
};
console.log(user.address.city); // "London" For “maybe missing” nested chains, use optional chaining (?.)
from the Control Flow chapter.
Try It Yourself
Exercise
Build a user object
// declare user here
console.log(user.greet());
💡 Show hint
✅ Show solution
const user = {
name: "Ada",
age: 36,
greet() {
return `Hello, ${this.name}`;
},
};
console.log(user.greet());
Up Next
Next: more on property shorthand, computed keys, and shorthand syntax.
JavaScript Object Properties →