Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

Object.prototype.valueOf()

BaselineWidely available

ThevalueOf() method ofObject instances converts thethis valueto an object. This method is meant to be overridden by derived objects for customtype conversion logic.

Try it

function MyNumberType(n) {  this.number = n;}MyNumberType.prototype.valueOf = function () {  return this.number;};const object1 = new MyNumberType(4);console.log(object1 + 3);// Expected output: 7

Syntax

js
valueOf()

Parameters

None.

Return value

Thethis value, converted to an object.

Note:In order forvalueOf to be useful during type conversion, it must return a primitive. Because all primitive types have their ownvalueOf() methods, callingaPrimitiveValue.valueOf() generally does not invokeObject.prototype.valueOf().

Description

JavaScript calls thevalueOf method toconvert an object to a primitive value. You rarely need to invoke thevalueOf method yourself; JavaScript automatically invokes it when encountering an object where a primitive value is expected.

This method is called in priority bynumeric conversion andprimitive conversion, butstring conversion callstoString() in priority, andtoString() is very likely to return a string value (even for theObject.prototype.toString() base implementation), sovalueOf() is usually not called in this case.

All objects that inherit fromObject.prototype (that is, all exceptnull-prototype objects) inherit thetoString() method. TheObject.prototype.valueOf() base implementation is deliberately useless: by returning an object, its return value will never be used by anyprimitive conversion algorithm. Many built-in objects override this method to return an appropriate primitive value. When you create a custom object, you can overridevalueOf() to call a custom method, so that your custom object can be converted to a primitive value. Generally,valueOf() is used to return a value that is most meaningful for the object — unliketoString(), it does not need to be a string. Alternatively, you can add a[Symbol.toPrimitive]() method, which allows even more control over the conversion process, and will always be preferred overvalueOf ortoString for any type conversion.

Examples

Using valueOf()

The basevalueOf() method returns thethis value itself, converted to an object if it isn't already. Therefore its return value will never be used by any primitive conversion algorithm.

js
const obj = { foo: 1 };console.log(obj.valueOf() === obj); // trueconsole.log(Object.prototype.valueOf.call("primitive"));// [String: 'primitive'] (a wrapper object)

Overriding valueOf for custom objects

You can create a function to be called in place of the defaultvalueOf method. Your function should take no arguments, since it won't be passed any when called during type conversion.

For example, you can add avalueOf method to your custom classBox.

js
class Box {  #value;  constructor(value) {    this.#value = value;  }  valueOf() {    return this.#value;  }}

With the preceding code in place, any time an object of typeBox is used in a context where it is to be represented as a primitive value (but not specifically a string), JavaScript automatically calls the function defined in the preceding code.

js
const box = new Box(123);console.log(box + 456); // 579console.log(box == 123); // true

An object'svalueOf method is usually invoked by JavaScript, but you can invoke it yourself as follows:

js
box.valueOf();

Using unary plus on objects

Unary plus performsnumber coercion on its operand, which, for most objects without[Symbol.toPrimitive](), means calling itsvalueOf(). However, if the object doesn't have a customvalueOf() method, the base implementation will causevalueOf() to be ignored and the return value oftoString() to be used instead.

js
+new Date(); // the current timestamp; same as new Date().getTime()+{}; // NaN (toString() returns "[object Object]")+[]; // 0 (toString() returns an empty string list)+[1]; // 1 (toString() returns "1")+[1, 2]; // NaN (toString() returns "1,2")+new Set([1]); // NaN (toString() returns "[object Set]")+{ valueOf: () => 42 }; // 42

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-object.prototype.valueof

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp