Object Methods
Objects can hold behaviour as well as data. Built-in Object helpers let you loop over, transform and protect objects.
On this page · 5 min · Beginner
On this page
- Learning objectives
- Explanation
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Beginner
- Updated
- Oct 27, 2026
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#
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]]));
170
pen: ₹20
book: ₹150
dark
{ a: 1, b: 2 }Step-by-step explanation#
- total() is a method; this.items refers to the cart’s own items.
- Object.entries gives [key, value] pairs, which destructure neatly in the loop.
- Changes to a frozen object are ignored (and throw an error in strict mode).
- 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#
total: () => this.items fails because arrows do not have their own this.
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
const scores = { asha: 82, ravi: 67, meera: 91 };
for (const [name, s] of Object.entries(scores)) console.log(name, s >= 70 ? 'pass' : 'retry');
Mini quiz#
What does Object.entries({ a: 1 }) return?
Want more? Practise all JavaScript MCQs · JavaScript interview questions
My notes#
Saved in this browser.