jsguides

Object

Description

The Object class is the foundation of JavaScript’s object-oriented system. Every object in JavaScript inherits properties and methods from Object.prototype, making it the most fundamental constructor in the language. Objects in JavaScript are collections of key-value pairs, where keys are strings (or Symbols) and values can be any data type including other objects, functions, or primitive values.

The Object class provides static methods for working with objects directly, rather than through object instances. These methods allow you to create objects, copy properties, define property descriptors, and inspect object structures. Understanding Object is essential because it underlies nearly everything in JavaScript—from plain objects and arrays to function prototypes and class instances.

Methods

Object.create()

Creates a new object with a specified prototype and optional properties.

const person = {
  isHuman: false,
  printIntroduction: function() {
    console.log(`My name is ${this.name}. Am I human? ${this.isHuman}`);
  }
};

const me = Object.create(person);
me.name = 'Matthew';
me.isHuman = true;

me.printIntroduction();
// Output: "My name is Matthew. Am I human? true"

Object.create() gives you direct control over the prototype chain, which is useful when you need behaviour shared across many instances without using constructor functions or classes. The created object inherits methods from the prototype, but any own properties you add after creation live only on the instance.

Object.assign()

Copies enumerable own properties from one or more source objects to a target object.

const target = { a: 1, b: 2 };
const source = { b: 4, c: 5 };

const returnedTarget = Object.assign(target, source);

console.log(target);
// Expected output: Object { a: 1, b: 4, c: 5 }

console.log(returnedTarget);
// Expected output: Object { a: 1, b: 4, c: 5 }

Unlike Object.create(), which sets up the prototype, Object.assign() copies own enumerable properties directly from one or more source objects onto a target. It mutates the target in place and also returns it, so you can either use the return value or keep a separate reference. This makes assign() the right choice for merging defaults with user-supplied options.

Object.keys()

Returns an array of a given object’s own enumerable property names.

const object1 = {
  a: 'somestring',
  b: 42,
  c: false
};

console.log(Object.keys(object1));
// Expected output: Array ["a", "b", "c"]

After you have merged properties with Object.assign(), Object.keys() helps you inspect what keys are present on the resulting object. It returns only the object’s own enumerable string-keyed properties, excluding inherited ones and Symbol keys. For key-value pairs instead of just keys, use Object.entries().

Object.freeze()

Freezes an object, preventing modifications to its properties.

const obj = {
  prop: 42
};

Object.freeze(obj);

obj.prop = 33;
// Throws an error in strict mode

console.log(obj.prop);
// Expected output: 42

Object.freeze() makes an object shallowly immutable: existing properties cannot be changed, new properties cannot be added, and the prototype cannot be reassigned. Attempts to modify a frozen object silently fail outside strict mode; in strict mode they throw a TypeError. After freezing, Object.keys() and Object.entries() still iterate normally — freeze blocks mutation, not reading.

Object.entries()

Returns an array of key-value pairs for each enumerable own property.

const object1 = {
  a: 'somestring',
  b: 42
};

for (const [key, value] of Object.entries(object1)) {
  console.log(`${key}: ${value}`);
}
// Output: "a: somestring"
// Output: "b: 42"

Object as the base prototype

Every plain object in JavaScript inherits from Object.prototype unless it is created with a different prototype. That shared ancestry is why methods such as toString(), hasOwnProperty(), and valueOf() are available on so many values. Understanding that relationship helps explain why objects can behave both like simple dictionaries and like richer instances with inherited behavior.

This also explains why Object works as both a constructor and a namespace of utility functions. The static methods on Object help you create and inspect objects without needing an existing instance. That split between static helpers and prototype methods is one of the core patterns in JavaScript.

Choosing the right Object method

Different Object methods answer different questions. Object.keys() tells you which properties exist, Object.entries() gives you key-value pairs, Object.assign() copies values, and Object.freeze() prevents further changes. Picking the right one makes the code clearer than reaching for a generic loop and hand-rolling the same behavior.

When you need to work with prototypes, Object.create() is especially important because it lets you define inheritance directly. That is a more precise tool than copying properties, and it can be useful when you want shared behavior without using classes. In practice, many codebases use both approaches depending on whether they need composition, inheritance, or plain data objects.

See Also

  • Map — Key-value collections with better iteration support
  • Object.keys() — Get an array of an object’s own enumerable property names