Course contentsShow
← JavaScript
Lesson 707 of 2,72317. Prototypes and InheritanceFree lesson

Object.prototype Methods

Exploring toString, valueOf, and other methods inherited by all objects.

Object.prototype Methods

You'll learn which built-in methods every single JavaScript object inherits automatically.

What Are Object.prototype Methods?

Every object in JavaScript inherits methods from Object.prototype, which sits at the very top of the prototype chain. These methods are available on every object you create, whether it's a plain object literal, array, function, or even a custom object. Think of them as "universal tools" built into the language itself.

Common Inherited Methods

toString()

Converts an object to a string representation. By default, it returns "[object Object]" for plain objects:

const user = { name: "Alice" };
console.log(user.toString()); // "[object Object]"

You can override this to provide custom string representations.

valueOf()

Returns the primitive value of an object. For plain objects, it typically returns the object itself:

const obj = { value: 42 };
console.log(obj.valueOf()); // { value: 42 }

This becomes more useful when JavaScript needs to convert objects to primitives (like in mathematical operations).

hasOwnProperty()

You've already seen this one! It checks if a property belongs directly to the object (not inherited):

const person = { age: 30 };
console.log(person.hasOwnProperty("age")); // true
console.log(person.hasOwnProperty("toString")); // false

Why This Matters

Because these methods exist on Object.prototype, they're accessible everywhere. However, you typically won't call toString() or valueOf() directly—JavaScript uses them internally during type conversions.

Key Takeaway: Every JavaScript object inherits universal methods like toString(), valueOf(), and hasOwnProperty() from Object.prototype, making them available on all objects regardless of how they were created.