JavaScript · Data Structures

Object Methods

Objects can hold behaviour as well as data. Built-in Object helpers let you loop over, transform and protect objects.

Beginner 5 min Lesson 14 of 49 Runnable examples
Before you start:Objects
0/49 in JavaScript
On this page · 5 min · Beginner

Learning objectives#

  • Write methods that use this
  • Loop over objects with Object.entries
  • Build objects with Object.fromEntries
  • Prevent changes with Object.freeze

Explanation#

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

Object.keys, Object.values and Object.entries turn an object into arrays you can loop over. Object.fromEntries does the reverse.

Object.assign(target, source) and the spread syntax copy properties. Object.freeze makes an object read-only (shallowly).

Code example: A shopping cart#

JavaScriptA shopping cart Runnable
const cart = {
  items: [{ name: 'Pen', price: 20 }, { name: 'Book', price: 150 }],
  total() {
    return this.items.reduce((sum, item) => sum + item.price, 0);
  },
};
console.log(cart.total());


const prices = { pen: 20, book: 150 };
for (const [item, price] of Object.entries(prices)) {
  console.log(`${item}: ₹${price}`);
}


const config = Object.freeze({ theme: 'dark' });
config.theme = 'light';          // ignored: the object is frozen
console.log(config.theme);


console.log(Object.fromEntries([['a', 1], ['b', 2]]));
Expected output
170
pen: ₹20
book: ₹150
dark
{ a: 1, b: 2 }
Opens this example in the full playground

Step-by-step explanation#

  1. total() is a method; this.items refers to the cart’s own items.
  2. Object.entries gives [key, value] pairs, which destructure neatly in the loop.
  3. Changes to a frozen object are ignored (and throw an error in strict mode).
  4. Object.fromEntries turns pairs back into an object.

Real-world use case#

Turning a settings object into a list of form fields with Object.entries, or freezing configuration so no code can change it accidentally.

Common mistakes#

Arrow functions as methods

total: () => this.items fails because arrows do not have their own this.

Expecting freeze to be deep

Object.freeze only protects the top level; nested objects can still change.

Best practices#

  • Use method shorthand (total() { … })
  • Use Object.entries for key-value loops
  • Freeze configuration constants
  • Prefer returning new objects over mutating shared ones

Remember this#

Method = function that lives inside an object. this = “the object before the dot”.

keys → names, values → contents, entries → both.

freeze = look but don’t touch (top level only).

Practice exercise#

Your task: Given scores = { asha: 82, ravi: 67, meera: 91 }, print each name with "pass" (≥ 70) or "retry".

Show solution
JavaScript Runnable
const scores = { asha: 82, ravi: 67, meera: 91 };
for (const [name, s] of Object.entries(scores)) console.log(name, s >= 70 ? 'pass' : 'retry');
Opens this example in the full playground

Mini quiz#

Object Methods quizMultiple choice Question 1 of 1

What does Object.entries({ a: 1 }) return?

Score so far: 0/0

Want more? Practise all JavaScript MCQs · JavaScript interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.