Skip to content

Methods

Methods are functions that are properties of objects. They can access the object’s other properties via the this keyword.

const user = {
name: 'Alice',
greet() { // shorthand — no 'function' keyword
return `Hi, I'm ${this.name}`;
}
};
user.greet(); // "Hi, I'm Alice"
const user = {
name: 'Alice',
greet: function() {
return `Hi, I'm ${this.name}`;
}
};
const user = {
name: 'Alice',
greet: () => {
return `Hi, I'm ${this.name}`; // this is NOT user!
}
};
// Arrow functions don't have their own 'this'
const calculator = {
value: 0,
add(n) { this.value += n; return this; },
subtract(n) { this.value -= n; return this; },
getResult() { return this.value; }
};
calculator.add(5).subtract(2).getResult(); // 3
  • Methods are functions stored as object properties
  • Use shorthand syntax: methodName() {}
  • Use this to access other properties
  • Arrow functions don’t bind their own this