Home
 › Learn
 › JavaScript

Learn JavaScript Objects & Methods | Complete Guide

A comprehensive guide to working with JavaScript objects and the essential methods for object manipulation, inspection, and transformation.

Updated At: December 2021
Learn JavaScript Objects & Methods | Complete Guide

Here is the complete list of JavaScript objects methods categorized by their primary functions.

What is a JavaScript Object?

A JavaScript object is a collection of key-value pairs used to store related data and functionality. Objects are fundamental building blocks in JavaScript for organizing and managing data.

Basic object creation:

const person = {
  name: "John",
  age: 30,
  city: "New York",
  greet: function() {
    return `Hello, I'm ${this.name}`;
  }
};

console.log(person.name);        // "John"
console.log(person.greet());     // "Hello, I'm John" 

Object.keys()

Returns an array of enumerable property names (keys) from an object. Does not include non-enumerable properties or inherited properties.​

Syntax:

Object.keys(obj);

Example:

const user = { name: "Alice", age: 25, email: "alice@example.com" };
const keys = Object.keys(user);

console.log(keys); // ["name", "age", "email"]
Use when you need to iterate over property names or check what keys an object contains.

Object.values()

Returns an array of enumerable property values from an object.​

Syntax:

Object.values(obj);

Example:

const user = { name: "Alice", age: 25, city: "London" };
const values = Object.values(user);

console.log(values); // ["Alice", 25, "London"]
Use when you need just the values without the keys.

Object.entries()

Returns an array of [key, value] pairs from an object. Useful for transforming objects or iterating with both keys and values.​

Syntax:

Object.entries(obj);

Example:

const person = { name: "Bob", age: 30, occupation: "Engineer" };
const entries = Object.entries(person);

console.log(entries);
// [["name", "Bob"], ["age", 30], ["occupation", "Engineer"]]

Using with for...of:

for (const [key, value] of Object.entries(person)) {
  console.log(`${key}: ${value}`);
}
// Output:
// name: Bob
// age: 30
// occupation: Engineer

Object.fromEntries()

Converts an array of [key, value] pairs back into an object. Pairs well with Object.entries() for transformations.​

Example – doubling all numeric values:

const prices = { apple: 10, banana: 5, orange: 8 };

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

console.log(doubled); // { apple: 20, banana: 10, orange: 16 }

Object.assign()

Copies all enumerable properties from one or more source objects to a target object. Useful for merging objects or creating shallow copies.​

Syntax:

Object.assign(target, source1, source2, ...);

Example – merging objects:

const defaults = { theme: "light", language: "en" };
const userSettings = { theme: "dark" };

const finalSettings = Object.assign({}, defaults, userSettings);
console.log(finalSettings); // { theme: "dark", language: "en" }

Object.create()

Creates a new object with a specified prototype object. Useful for prototypal inheritance.

Example:

const proto = { greet() { return "Hello!"; } };
const obj = Object.create(proto);

console.log(obj.greet()); // "Hello!"
console.log(Object.getPrototypeOf(obj) === proto); // true

Object.freeze()

Prevents any changes to an object — no modifications, additions, or deletions of properties. Only a shallow freeze.​

Example:

const person = { name: "Alice", age: 30 };
Object.freeze(person);

person.age = 31; // Fails silently (no error in non-strict mode)
person.email = "alice@email.com"; // Fails silently

console.log(person); // { name: "Alice", age: 30 }

Check if frozen:

console.log(Object.isFrozen(person)); // true

Important: Object.freeze() is shallow — nested objects can still be modified:

const user = {
  name: "Bob",
  preferences: { theme: "dark" }
};

Object.freeze(user);
user.preferences.theme = "light"; // This works!

console.log(user.preferences.theme); // "light"

Deep freeze solution:

function deepFreeze(obj) {
  Object.keys(obj).forEach(prop => {
    if (typeof obj[prop] === 'object' && obj[prop] !== null) {
      deepFreeze(obj[prop]);
    }
  });
  return Object.freeze(obj);
}

const user = { name: "Carol", prefs: { theme: "dark" } };
deepFreeze(user);
user.prefs.theme = "light"; // Now this fails!

Object.seal()

Prevents adding or deleting properties, but allows modifications to existing properties.​

Example:

const obj = { name: "Dave", age: 25 };
Object.seal(obj);

obj.age = 26;        // Works
obj.email = "dave@email.com"; // Fails silently
delete obj.age;      // Fails silently

console.log(obj); // { name: "Dave", age: 26 }

Object.getOwnPropertyNames()

Returns an array of all properties (enumerable AND non-enumerable) defined directly on an object. Does not include inherited properties or symbols.​

Example:

const obj = { name: "Eve" };
Object.defineProperty(obj, 'id', {
  value: '123',
  enumerable: false  // Non-enumerable
});

console.log(Object.keys(obj));              // ["name"]
console.log(Object.getOwnPropertyNames(obj)); // ["name", "id"]
Use when you need all properties including non-enumerable ones.

Object.defineProperty()

Defines a new property or modifies an existing one with precise control over configurability, enumerability, and writeability.​

Syntax:

Object.defineProperty(obj, propertyName, descriptor);

Example – readonly property:

const person = {};

Object.defineProperty(person, 'ssn', {
  value: '123-45-6789',
  writable: false,      // Cannot be modified
  enumerable: false,    // Won't show in Object.keys()
  configurable: false   // Cannot be deleted or reconfigured
});

person.ssn = '000-00-0000'; // Fails silently
console.log(person.ssn);    // "123-45-6789"

Object.getPrototypeOf()

Returns the prototype object of a given object.

Example:

const arr = [];
console.log(Object.getPrototypeOf(arr) === Array.prototype); // true

Object.hasOwn()

Checks if an object has a specific own property (modern replacement for obj.hasOwnProperty()).

Example:

const obj = { name: "Frank", age: 28 };

console.log(Object.hasOwn(obj, 'name')); // true
console.log(Object.hasOwn(obj, 'email')); // false

Best Practices

  1. Use Object.keys(), Object.values(), Object.entries() for iteration over enumerable properties.

  2. Use Object.assign() for shallow object merging or copying.

  3. Use Object.freeze() for immutable objects, and deepFreeze() for nested structures.

  4. Use Object.defineProperty() when you need fine control over property attributes.

  5. Prefer Object.hasOwn() over hasOwnProperty() to avoid prototype chain surprises.

  6. Use Object.getOwnPropertyNames() when you need all properties including non-enumerable ones.


JavaScript

    React

      NextJS

        HTML

          CSS

            Sign up for our newsletter.

            Copyright © theHardCoder 2021 - 2025