TetSharing
JavaScript tutorial · 19 chapters
  1. Get started
  2. let, const and var
  3. Data types
  4. Operators and equality
  5. Strings
  6. Template literals
  7. if, else and switch
  8. Loops
  9. Functions
  10. Arrow functions
  11. Arrays
  12. map, filter, reduce
  13. Objects
  14. Destructuring and spread
  15. Classes
  16. Errors and try...catch
  17. Promises
  18. async and await
  19. JSON

Final JavaScript quiz
Cheat sheet: javascript array methods

Chapter 13 of 19 · By Ethan Cole · Updated November 21, 2026

JavaScript Objects: Properties, Methods and Keys

An object groups related values under named properties: { name: "Noah", city: "Seattle" }. Where an array answers "what is at position 2?", an object answers "what is the city?". Objects are the building block of almost all JavaScript data, from settings to API responses.

Reading and writing properties

Dot notation, user.name, is the usual way. Bracket notation, user["favorite color"], is required when the key contains spaces or dashes, or when the key is in a variable, as in user[key]. Assigning to a property that does not exist creates it, and assigning to an existing one replaces its value. delete user.age removes a property.

Reading a missing property returns undefined instead of throwing. But reading a property of undefined does throw a TypeError. Optional chaining, user.email?.length, stops and returns undefined if the part before ?. is null or undefined.

JavaScript · editable

Expected output

Noah teal
{name: "Noah", city: "Portland", age: 31}
true undefined undefined
Portland
["name", "city", "age"] 3
name: Noah
city: Portland
age: 31
(1, 2)

Inspecting an object

  • "age" in user checks whether a property exists.
  • Object.keys(obj) returns the property names, Object.values(obj) the values, and Object.entries(obj) an array of [key, value] pairs.
  • for (const [k, v] of Object.entries(user)) loops over keys and values together, using array destructuring from the next chapter.

Properties keep insertion order for string keys, with one exception: keys that look like whole numbers come first, in numeric order.

Methods and this

A property whose value is a function is a method. The shorthand toString() { ... } defines one. Inside a method called as point.toString(), this refers to point. Template literals call toString() automatically, which is why ${point} prints (1, 2).

Objects are references

Assigning an object to another variable does not copy it; both names point at the same object. Copy with spread, { ...user }, which is covered in the next chapter.

Common mistakes

  • Using dot notation with a variable: user.key looks for a property literally named key.
  • Reading deep properties without ?. and crashing on undefined.
  • Comparing objects with ===, which checks identity, not contents.

Summary

  • Objects map property names to values; use dot or bracket notation.
  • Missing properties are undefined; ?. guards deep reads.
  • Object.keys, values and entries list an object's contents.
  • Functions stored in properties are methods, with this as the object.

Quiz: Objects

1. How do you read a property whose name is stored in the variable k?

Bracket notation evaluates the expression inside the brackets.

2. What does ({}).missing return?

Reading a missing property gives undefined.

3. What does Object.keys({ a: 1, b: 2 }) return?

Object.keys returns the property names.

Official docs

Written under the pen name Ethan Cole with AI-assisted drafting. Every code example on this page is executed by our test suite before publishing. Found a mistake? Tell us.