Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Object.getPrototypeOf: the immediate prototype of a value

Object.getPrototypeOf returns an object’s current immediate prototype, or null for an object with no prototype.

Version: ECMAScript 5 onward; primitive coercion since ECMAScript 2015; example target: Node.js 24.22.2

Syntax

Object.getPrototypeOf(obj)

Object.getPrototypeOf(obj) performs ToObject and invokes the internal [[GetPrototypeOf]] method. For an object it returns the immediate prototype reference or null; it neither walks the whole chain nor copies the prototype. Since ECMAScript 2015, primitives other than null and undefined are boxed, so a string points to String.prototype.

Use it to inspect inheritance, recognize Object.create(null) dictionaries, or examine class-created objects. The returned reference describes the state at the call; a later Object.setPrototypeOf can change the next result. This operation does not read ordinary properties or invoke their getters.

The method has existed since ECMAScript 5, whose primitive arguments threw TypeError. Today null and undefined still throw TypeError. A Proxy can run its getPrototypeOf trap; the trap must return an object or null, and for a non-extensible target it must report the target’s actual prototype, otherwise TypeError is thrown.

Examples

const proto = { kind: "record" };
const value = Object.create(proto);
const root = Object.create(null);

console.log(Object.getPrototypeOf(value) === proto, Object.getPrototypeOf(root));
console.log(Object.getPrototypeOf("x") === String.prototype);

const fixed = Object.preventExtensions({});
const proxy = new Proxy(fixed, { getPrototypeOf() { return null; } });
try { Object.getPrototypeOf(proxy); } catch (error) { console.log(error.name); }
try { Object.getPrototypeOf(null); } catch (error) { console.log(error.name); }

Result

true null
true
TypeError
TypeError

The example reads an ordinary and null prototype, shows string boxing, and exercises two TypeError cases.

Sources