Academic Block

Object Methods Get Certified

JavaScript provides a rich set of built-in methods on the Object constructor that make working with objects much more convenient. These methods help you inspect, copy, merge, lock, and create objects safely.

In this lesson you will learn the most useful Object methods, when to use each one, and the common mistakes to avoid.

Object.keys(), Object.values(), Object.entries()

These three methods turn an object’s contents into arrays so you can easily loop over them or transform them.

What this code does: Object.keys returns an array of the object’s own property names. Object.values returns the values, and Object.entries returns key-value pairs.

const product = {
    name: "Wireless Mouse",
    price: 25,
    inStock: true
};
console.log(Object.keys(product));
console.log(Object.values(product));
console.log(Object.entries(product));

What this code does: Looping over Object.entries is a clean way to process every property. Each iteration gives you both the key and the value.

const scores = { Alice: 92, Ben: 87, Chloe: 95 };
for (const [name, score] of Object.entries(scores)) {
    console.log(name + " scored " + score);
}

Object.fromEntries()

Object.fromEntries() does the opposite of Object.entries(): it builds an object from an array of key-value pairs.

What this code does: Object.fromEntries turns an array of pairs back into an object. This is useful after transforming entries with map or filter.

const pairs = [
    ["brand", "Nike"],
    ["size", 10],
    ["color", "black"]
];
const shoe = Object.fromEntries(pairs);
console.log(shoe);

What this code does: A common pattern: convert to entries, transform the values, then rebuild the object. Every price is increased by 10%.

const prices = { apple: 1.2, banana: 0.8, orange: 1.5 };
const inflated = Object.fromEntries(
    Object.entries(prices).map(([fruit, price]) => [fruit, price * 1.1])
);
console.log(inflated);

Object.assign()

Object.assign() copies properties from one or more source objects into a target object. It is commonly used to merge objects or create shallow copies.

What this code does: Object.assign copies properties into the first argument (the target). Passing an empty object as the target creates a new merged object without mutating the sources.

const defaults = { theme: "light", fontSize: 14 };
const userSettings = { theme: "dark" };
const settings = Object.assign({}, defaults, userSettings);
console.log(settings);
console.log(defaults); // unchanged

What this code does: Later sources override earlier ones. The final settings keep the dark theme and the default notifications value.

const defaults = { theme: "light", notifications: true };
const userPrefs = { theme: "dark" };
const finalSettings = Object.assign({}, defaults, userPrefs);
console.log(finalSettings);

Object.freeze()

Object.freeze() prevents any changes to an object: properties cannot be added, removed, or modified afterward.

What this code does: Object.freeze() locks an object: you cannot add, remove, or change properties. Attempts to update founded or add city are ignored (in non-strict mode).

const teamRoster = Object.freeze({
    name: "Ravens FC",
    founded: 1998
});
teamRoster.founded = 2000;
teamRoster.city = "Denver";
console.log(teamRoster);

Both attempted changes are silently ignored outside strict mode (or throw a TypeError inside strict mode), and teamRoster remains exactly as it was.

Object.isFrozen()

What this code does: Object.isFrozen() checks whether an object is frozen. The roster returns true; a plain object returns false.

const teamRoster = Object.freeze({ name: "Ravens FC" });
const otherTeam = { name: "Falcons FC" };
console.log(Object.isFrozen(teamRoster));
console.log(Object.isFrozen(otherTeam));

Note that Object.freeze() is shallow — it only protects the object’s own properties, not the contents of any nested objects inside it.

Object.seal()

Object.seal() is a bit less strict than freeze(): it prevents adding or removing properties, but still allows existing properties to be modified.

What this code does: Object.seal() prevents adding or removing properties, but existing ones can still be changed. Difficulty updates; subtitles cannot be added.

const gameSettings = Object.seal({
    difficulty: "normal",
    musicVolume: 80
});
gameSettings.difficulty = "hard";
gameSettings.subtitles = true;
console.log(gameSettings);

The difficulty value updates successfully, but the new subtitles property is silently rejected, since sealing prevents adding new properties.

Object.freeze() vs. Object.seal()

Operation Object.freeze() Object.seal()
Add new properties Blocked Blocked
Remove properties Blocked Blocked
Modify existing properties Blocked Allowed

Object.create()

Object.create() creates a brand-new object with a specified prototype, allowing that object to inherit properties and methods from another object.

What this code does: Object.create() makes an object that inherits from a given prototype. The scooter gets describe from the prototype and uses its own make and model.

const vehiclePrototype = {
    describe: function () {
        return this.make + " " + this.model;
    }
};
const scooter = Object.create(vehiclePrototype);
scooter.make = "Zipline";
scooter.model = "E200";
console.log(scooter.describe());

The describe method isn’t copied onto scooter directly — it’s inherited through the prototype chain.

Object.getPrototypeOf()

Object.getPrototypeOf() returns the prototype of a given object, which can be useful for inspecting where an object’s inherited properties come from.

What this code does: Object.getPrototypeOf() returns the object’s prototype. Confirming it matches vehiclePrototype shows the inheritance link.

const vehiclePrototype = {
    wheels: 4
};
const scooter = Object.create(vehiclePrototype);
console.log(Object.getPrototypeOf(scooter) === vehiclePrototype);

Comparing Objects

Objects are compared by reference, not by their contents. Object.is() behaves almost identically to ===, but with slightly different handling for a few special numeric values.

What this code does: Objects are compared by reference, not by contents. Two separate objects with the same data are not equal; assigning one variable to another shares the same reference.

const menuA = { item: "Latte", price: 4.75 };
const menuB = { item: "Latte", price: 4.75 };
const menuC = menuA;
console.log(menuA === menuB);
console.log(menuA === menuC);
console.log(Object.is(menuA, menuC));

Even though menuA and menuB have identical contents, they are different objects in memory, so the comparison is false. menuC, however, points to the exact same object as menuA.

Practical Example

Let’s combine several Object methods to merge default settings with user overrides, then lock the result so it can’t be accidentally changed later.

What this code does: Merge defaults with user choices, then freeze the result. The final profile is locked so nothing can change it later.

const defaultProfile = {
    displayName: "Guest",
    language: "en",
    theme: "light"
};
const userChoices = {
    displayName: "Michael",
    theme: "dark"
};
const finalProfile = Object.freeze(
    Object.assign({}, defaultProfile, userChoices)
);
console.log(finalProfile);
console.log(Object.isFrozen(finalProfile));

Common Mistakes With Object Methods

  • Forgetting to pass an empty object as the first argument to Object.assign(), accidentally overwriting one of the source objects.
  • Assuming Object.freeze() also protects nested objects, when it only prevents changes at the top level.
  • Comparing two separate objects with identical contents using === and expecting true.
  • Confusing Object.seal() with Object.freeze(), and being surprised that sealed objects can still have their existing properties changed.
  • Forgetting that changes to a frozen object fail silently outside of strict mode, making the mistake easy to miss.

Best Practices for Object Methods

  • Prefer the spread operator ({ ...obj }) or Object.assign({}, ...) for merging objects without mutating the originals.
  • Use Object.freeze() for configuration or constant objects that should never change after creation.
  • Reach for Object.entries() combined with Object.fromEntries() when you need to transform an object’s values while keeping the same keys.
  • Remember that object equality checks compare references, not contents — write a dedicated comparison function if you need to compare values deeply.
  • Use Object.isFrozen() or Object.isSealed() when debugging unexpected “changes not taking effect” issues.

Quick Summary

  • Object.keys(), Object.values(), and Object.entries() extract an object’s keys, values, or both as arrays.
  • Object.fromEntries() builds an object back up from an array of key-value pairs.
  • Object.assign() merges properties from source objects into a target object.
  • Object.freeze() locks an object completely; Object.seal() allows existing properties to still be modified.
  • Object.create() builds an object with a specific prototype for inheritance.
  • Objects are compared by reference, not by their contents.

Try It Yourself

Modify the code below. Try adding another override property, or checking whether the final result is frozen.

What this code does: Practice example: merge a base recipe with customizations, then inspect the result with Object.keys. Try adding more overrides or freezing the result.

const baseRecipe = { name: "Pancakes", servings: 4, spicy: false };
const customizations = { servings: 8 };
const finalRecipe = Object.assign({}, baseRecipe, customizations);
console.log(finalRecipe);
console.log(Object.keys(finalRecipe));
Tip: When merging configuration objects, put your defaults first and overrides last in Object.assign() — later arguments always win, which mirrors how most configuration systems are expected to behave.

Understanding these built-in Object methods is useful before moving on to classes, prototypes, and more advanced JavaScript concepts.

🧪 Test Your JavaScript Code

Edit the code on the left and click “Run Code” to see the console output on the right.

📝 JavaScript Code
👁 Console Output

Click “Run Code” to see the output here.

Ctrl + Enter to run code Esc to close