BeginnerCoder

Lesson 10 — Objects & methods

Behavior on data

Objects can hold methods — functions stored as properties. Write them with the short form name() { ... } inside the object braces, separated from other properties by commas. Inside a method, this refers to the object itself, so this.name reads its own name.

const dog = {
  sound: "woof",
  speak() { return this.sound; },
};

Why it matters: keeping data and the functions that use it together is the basis of classes and most JS libraries.

Want it another way?
Task

Add a greet() method to p that returns "Hi " + the name, then log p.greet().

Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.