SKU // 51

Object Methods

A method is a function stored as a property of an object. Properties describe what an object is; methods describe what it can do.

// Defining a Method

const dog = {
  name: "Rex",
  bark: function() {
    return "Woof!";
  }
};

console.log(dog.bark());
Woof!

// Shorthand Syntax

Modern JavaScript lets you leave out the colon and the word function. This is the style you will see most:

const dog = {
  name: "Rex",
  bark() {
    return "Woof!";
  }
};

// Calling Needs Parentheses

console.log(dog.bark());   // "Woof!"  (calls the method)
console.log(dog.bark);     // the function itself, not its result
Woof! ƒ bark() { return "Woof!"; }

// Methods with Parameters

const calculator = {
  add(a, b)      { return a + b; },
  subtract(a, b) { return a - b; },
  average(...nums) {
    let sum = 0;
    for (const n of nums) sum += n;
    return sum / nums.length;
  }
};

console.log(calculator.add(4, 5));
console.log(calculator.average(80, 90, 100));
9 90

Grouping related functions inside one object keeps them organized. This is exactly what the built-in Math object does with Math.round, Math.max, and the rest.

// Methods That Use the Object's Own Data

The real power of methods is working with the object's own properties. The keyword this means "the object this method was called on":

const account = {
  owner: "Jordan",
  balance: 100,

  deposit(amount) {
    this.balance += amount;
    return this.balance;
  },

  summary() {
    return `${this.owner} has $${this.balance}`;
  }
};

account.deposit(50);
console.log(account.summary());
Jordan has $150
Note
this is important enough, and tricky enough, to get its own lesson next. For now: inside a method, this.balance means "this object's balance."

// Adding a Method Later

Methods are just properties, so you can add one after the object is created:

const robot = { name: "Unit 7" };

robot.introduce = function() {
  return `I am ${this.name}`;
};

console.log(robot.introduce());
I am Unit 7

// Do Not Use Arrow Functions as Methods

Arrow functions do not get their own this, so they cannot see the object:

const cat = {
  sound: "meow",
  speakGood() { return this.sound; },
  speakBad: () => this.sound
};

console.log(cat.speakGood());   // "meow"
console.log(cat.speakBad());    // undefined
meow undefined
Rule
Use the shorthand name() { } syntax for methods. Save arrow functions for callbacks inside methods.

// Built-in Methods You Already Know

"hello".toUpperCase()        // string method
(3.14159).toFixed(2)          // number method
Math.random()                // Math object method
console.log()                 // console object method
Object.keys(obj)             // Object helper method

// Quick Check

What does this log?

const light = {
  on: false,
  toggle() { this.on = !this.on; }
};
light.toggle();
light.toggle();
light.toggle();
console.log(light.on);
Show answer
true. Each call flips on: false to true, true to false, false to true.

What does dog.bark (no parentheses) give you?

  1. "Woof!"
  2. The function itself
  3. undefined
  4. A TypeError
Show answer
B. Without () you get the method, not its result. Same rule as any function.

Why does speakBad: () => this.sound return undefined?

  1. Arrow functions cannot return strings
  2. Arrow functions do not have their own this, so it is not the object
  3. Methods must be declared with function
  4. sound is misspelled
Show answer
B. An arrow function uses the this of the code around the object, not the object itself. Use the speakGood() { } shorthand for methods.

// Try It

  1. Click Open Playground. Give lamp turnOn() and turnOff() methods
  2. Add a status() method that returns "Lamp is on" or "Lamp is off" using this
  3. Build a converter object with cToF(c) and fToC(f) methods
  4. Write one method as an arrow function and confirm it cannot read this
// Video Walkthroughs
Last updated: Oct 10, 2026, 12:52 AM ET