JavaScript Object Methods

Functions That Live on Objects

JavaScript Object Methods

Methods are functions stored as properties. Plus the `Object.*` helpers for iterating and copying.

4 min read Level 1/5 #objects#methods#this
What you'll learn
  • Define a method on an object
  • Use `Object.keys/values/entries` to iterate
  • Use `Object.assign` and spread to copy/merge

A method is a function stored as an object property. Inside a method, this refers to the object it was called on.

A method on an object script.js
const user = {
  name: "Ada",
  greet() {
    return `Hi, I'm ${this.name}`;
  },
};

console.log(user.greet());  // "Hi, I'm Ada"
▶ Preview: console

The greet() { … } shorthand from the previous lesson is the usual form. Don’t use arrow functions for object methods that need this — arrows inherit this from the surrounding scope, which is rarely what you want.

Built-in Object.* Helpers

These static methods on Object let you inspect and manipulate objects.

Object.keys, values, entries

Turn an object into an array of keys, values, or pairs.

Object.keys / values / entries script.js
const user = { name: "Ada", age: 36, role: "admin" };

console.log(Object.keys(user));    // ["name", "age", "role"]
console.log(Object.values(user));  // ["Ada", 36, "admin"]
console.log(Object.entries(user));
// [["name", "Ada"], ["age", 36], ["role", "admin"]]
▶ Preview: console

entries is the workhorse — pair it with for..of and destructuring to iterate cleanly:

Iterate properties script.js
const user = { name: "Ada", age: 36 };

for (const [key, value] of Object.entries(user)) {
  console.log(`${key} = ${value}`);
}
// "name = Ada"
// "age = 36"
▶ Preview: console

Object.fromEntries

The inverse: takes an array of [key, value] pairs and builds an object.

Object.fromEntries script.js
const pairs = [["name", "Ada"], ["age", 36]];
const obj = Object.fromEntries(pairs);
console.log(obj);  // { name: 'Ada', age: 36 }
▶ Preview: console

Combined with entries it’s a great way to transform every value:

Transform every value script.js
const prices = { apple: 1.50, bread: 3, cheese: 5.99 };

const doubled = Object.fromEntries(
  Object.entries(prices).map(([k, v]) => [k, v * 2])
);

console.log(doubled);
// { apple: 3, bread: 6, cheese: 11.98 }
▶ Preview: console

Object.assign

Copy properties from one or more sources into a target. It mutates the target and returns it.

Object.assign script.js
const target = { a: 1 };
const source = { b: 2, c: 3 };

Object.assign(target, source);
console.log(target);  // { a: 1, b: 2, c: 3 }
▶ Preview: console

In modern code, object spread is usually clearer:

const merged = { ...target, ...source };  // doesn't mutate

Object.freeze

Makes an object immutable — properties can’t be added, changed, or deleted.

Object.freeze script.js
const config = Object.freeze({ apiUrl: "https://example.com" });

// config.apiUrl = "hacker.example";   // silently fails (throws in strict)
console.log(config.apiUrl);             // "https://example.com"
▶ Preview: console

Object.hasOwn

The modern, safe way to ask “does this object have this OWN property?”

Object.hasOwn script.js
const user = { name: "Ada" };

console.log(Object.hasOwn(user, "name"));     // true
console.log(Object.hasOwn(user, "email"));    // false
console.log(Object.hasOwn(user, "toString")); // false  ← inherited, not own
▶ Preview: console

Up Next

Pull values out of objects with destructuring.

JavaScript Object Destructuring →