Sub topic 1
Creating Objects with Literals
An object in JavaScript is a collection of properties. Each property is a key paired with a value. This chapter covers how to create objects, read their properties, and change them.
Prerequisites
This chapter assumes you know basic JavaScript syntax (variables, functions, and control flow), primitive types such as strings and numbers, and how to run code in a browser console or Node.js.
Object Literals
An object literal is a list of key: value pairs inside braces. It is the most common way to create an object:
const book = { title: "Dune", pages: 412 };
const empty = {};Dot and Bracket Notation
Dot notation (obj.key) works only when the key is a valid identifier. Bracket notation (obj[expr]) accepts any expression, so it handles keys with spaces and keys stored in variables. Reading a property that does not exist returns undefined. It does not throw an error.
| Syntax | Key must be | Example |
|---|---|---|
| `obj.key` | A valid identifier, written in the code | `car.make` |
| `obj[expr]` | Any expression | `car["top speed"]`, `car[key]` |
Property Keys: Strings and Symbols
A property key is always either a string or a symbol. Any other value used as a key is converted to a string, so obj[1] and obj["1"] refer to the same property. A symbol is a unique value created with Symbol(). Code that doesn't hold a reference to the symbol can't accidentally create a property with the same key.
Shorthand and Computed Keys
Shorthand syntax lets you write { name } in place of { name: name }. A computed key, written as [expression], evaluates the expression and uses the result as the key.
const key = "color";
const name = "Corolla";
const id = Symbol("id");
const car = {
make: "Toyota",
name, // shorthand for name: name
[key]: "red", // computed key
"top speed": 180, // not a valid identifier, so it is quoted
1: "first",
[id]: 42 // symbol key
};
console.log(car.make); // "Toyota"
console.log(car.name); // "Corolla"
console.log(car["top speed"]); // 180
console.log(car[key]); // "red"
console.log(car[1] === car["1"]); // true
console.log(car[id]); // 42
console.log(car.model); // undefinedAdding, Updating, and Deleting Properties
When you assign to a key that doesn't exist, JavaScript adds a new property. When you assign to a key that already exists, it replaces the value. The delete operator removes the property itself.
const car = { make: "Toyota" };
car.year = 2020; // add
car.year = 2021; // update
car["top speed"] = 180;
delete car["top speed"];
console.log(car); // { make: "Toyota", year: 2021 }Common Errors
- Writing
car.keywhen you meancar[key]. The first form reads a property literally named"key". - Using dot notation with a key that contains a space or a hyphen. This is a syntax error.
- Expecting number keys to stay numbers. They are converted to strings.
Exercise
Given const f = "size"; const o = { [f + "Max"]: 10, f };, what are o.sizeMax, o.f, and o.size?
Show answer
o.sizeMax is 10 because the computed key evaluates to "sizeMax". o.f is "size" because the shorthand f creates the property f: "size". o.size is undefined because no property has the key "size".
Exercise
After const p = { a: 1 }; p.a = undefined;, does p still have the key "a"? How would you remove it?
Show answer
Yes. The key "a" still exists, and its value is undefined. Use delete p.a to remove the property.