SKU // 50

Object Properties

Properties are the named values inside an object. This lesson covers everything you can do with them: read, add, change, delete, check, and loop through.

// Dot vs Bracket Notation

const pet = { name: "Biscuit", species: "dog" };

console.log(pet.name);        // dot notation
console.log(pet["name"]);     // bracket notation: key as a string
Biscuit Biscuit

Both read the same property. Brackets are required in two situations:

// 1. The key is stored in a variable
let field = "species";
console.log(pet[field]);     // "dog"
console.log(pet.field);      // undefined  (looks for a key literally named "field")

// 2. The key is not a valid identifier
const data = { "first name": "Ada" };
console.log(data["first name"]);
dog undefined Ada
Rule of Thumb
Use dots when you know the key name while writing the code. Use brackets when the key comes from a variable, user input, or a loop.

// Adding and Changing

Assigning to a property that does not exist creates it. Assigning to one that does exist replaces its value:

const pet = { name: "Biscuit" };

pet.age = 3;             // added
pet.name = "Biscuit Jr."; // changed
pet["color"] = "tan";    // added with brackets

console.log(pet);
{name: "Biscuit Jr.", age: 3, color: "tan"}

// Deleting

delete pet.color;
console.log(pet.color);   // undefined
undefined

delete works with brackets too, delete pet["color"], and it removes both the value and the key. (Setting a property to undefined is different: the key stays.)

// Checking If a Property Exists

Reading a missing property gives undefined, but so does a property that exists and holds undefined. To check for the key itself, use in or Object.hasOwn:

const user = { name: "Sam", nickname: undefined };

console.log("name" in user);              // true
console.log("nickname" in user);          // true  (exists, value is undefined)
console.log("email" in user);             // false
console.log(Object.hasOwn(user, "name"));  // true
true true false true

// Safe Access with ?.

Reading a property of something that is undefined crashes. Optional chaining ?. stops and returns undefined instead:

const order = { id: 7 };   // no shipping info yet

// order.shipping.city;     TypeError: Cannot read properties of undefined
console.log(order.shipping?.city);                // undefined
console.log(order.shipping?.city ?? "Not set");  // "Not set"
undefined Not set
Note
?. pairs perfectly with ?? from the Logical lesson: try to read a nested value, and fall back to a default if any link in the chain is missing.

// Nested Objects

A property's value can be another object. Reach into it by chaining dots and brackets, in any mix:

const myObj = {
  name: "John",
  age: 30,
  myCars: {
    car1: "Ford",
    car2: "BMW",
    car3: "Fiat"
  }
};

console.log(myObj.myCars.car2);
console.log(myObj.myCars["car2"]);
console.log(myObj["myCars"]["car2"]);

let p1 = "myCars";
let p2 = "car2";
console.log(myObj[p1][p2]);          // both keys from variables
BMW BMW BMW BMW

// Shorthand Properties

When a variable has the same name as the key you want, write it once:

const name = "Lee";
const age = 17;

const longWay  = { name: name, age: age };
const shortWay = { name, age };             // same result

// Computed Keys

Brackets inside an object literal let a variable decide the key name:

const field = "score";
const result = { [field]: 95 };

console.log(result);   // {score: 95}
{score: 95}

// Looping Through Properties

const stock = { apples: 12, pears: 0, plums: 7 };

for (const key in stock) {
  console.log(`${key}: ${stock[key]}`);
}
apples: 12 pears: 0 plums: 7

Three built-in helpers turn an object into arrays you can work with:

console.log(Object.keys(stock));     // ["apples", "pears", "plums"]
console.log(Object.values(stock));   // [12, 0, 7]
console.log(Object.entries(stock));  // [["apples", 12], ["pears", 0], ["plums", 7]]

console.log(Object.keys(stock).length);   // 3  (how many properties)
["apples", "pears", "plums"] [12, 0, 7] [["apples", 12], ["pears", 0], ["plums", 7]] 3
// Total of all values
let total = 0;
for (const n of Object.values(stock)) total += n;
console.log(total);   // 19
19

// Quick Check

What does this log?

const car = { color: "red" };
const key = "color";
console.log(car.key, car[key]);
Show answer
undefined red. car.key looks for a property literally named "key". car[key] uses the variable's value, "color".

user.address may not exist. Which reads the zip code safely, with "none" as a fallback?

  1. user.address.zip || "none"
  2. user.address?.zip ?? "none"
  3. user?.address.zip
  4. "zip" in user
Show answer
B. ?. stops if address is missing, and ?? supplies the fallback. A crashes with a TypeError when there is no address; C still crashes at .zip; D checks the wrong object.

What does this log?

const scores = { ana: 90, ben: 80, cy: 70 };
console.log(Object.keys(scores).length);
Show answer
3. Object.keys gives an array of the three keys, and .length counts them.

// Try It

  1. Click Open Playground. Add, change, and delete one property of pet, logging it after each step
  2. Store a key name in a variable and read the property using brackets
  3. Use ?. and ?? to safely read user.address.zip when there is no address
  4. Loop over an object of test scores and print the average using Object.values
// Video Walkthroughs
Last updated: Oct 10, 2026, 12:52 AM ET