35 min·intermediate

Lesson 3

Objects & methods

Build a practical mental model for objects & methods and use it in working JavaScript.

By the end, you will be able to…

  • Explain objects & methods in your own words
  • Use objects & methods in a small program
  • Recognize and debug a common failure mode

Build the mental model

Objects group related data and behavior under named keys. A function stored as a property is a method, and inside a method this refers to the object it was called on.

Syntax and working example

Read the code from inputs to output. Predict what it prints, then run it and change one value.

Working example
Expected output: Hi, I'm Ada

Why this matters

Modeling a real-world thing (a user, a product, a task) as an object with clearly named properties makes code that reads like the domain it represents.

Real-world example

Every API response you'll fetch — a GitHub user, a weather report — arrives as exactly this kind of object with named fields.

Common mistakes

  • Accessing a missing property and getting undefined instead of checking first
  • Confusing dot notation (user.name) with bracket notation needed for dynamic keys (user[key])
  • Losing `this` by extracting a method into a standalone function reference

Try it yourself

Challenge

Add an isAdult() method to the user object based on an age property.

Practice

Knowledge check

Inside an object method, what does `this` typically refer to?

Summary

Objects group data and methods under named keys; methods use `this` to refer to their own object.