The valueOf()
method returns the primitive value of the specified object.
object.valueOf()
The primitive value of the specified object.
JavaScript calls the valueOf
method to convert an object to a primitive value. You rarely need to invoke the valueOf
method yourself; JavaScript automatically invokes it when encountering an object where a primitive value is expected.
By default, the valueOf
method is inherited by every object descended from Object
. Every built-in core object overrides this method to return an appropriate value. If an object has no primitive value, valueOf
returns the object itself.
You can use valueOf
within your own code to convert a built-in object into a primitive value. When you create a custom object, you can override Object.prototype.valueOf()
to call a custom method instead of the default Object
method.
You can create a function to be called in place of the default valueOf
method. Your function must take no arguments.
Suppose you have an object type MyNumberType
and you want to create a valueOf
method for it. The following code assigns a user-defined function to the object's valueOf
method:
MyNumberType.prototype.valueOf = function() { return customPrimitiveValue; };
With the preceding code in place, any time an object of type MyNumberType
is used in a context where it is to be represented as a primitive value, JavaScript automatically calls the function defined in the preceding code.
An object's valueOf
method is usually invoked by JavaScript, but you can invoke it yourself as follows:
myNumberType.valueOf()
Note: Objects in string contexts convert via the toString()
method, which is different from String
objects converting to string primitives using valueOf
. All objects have a string conversion, if only "[object type]
". But many objects do not convert to number, boolean, or function.
function MyNumberType(n) { this.number = n; } MyNumberType.prototype.valueOf = function() { return this.number; }; var myObj = new MyNumberType(4); myObj + 3; // 7
Specification | Status | Comment |
---|---|---|
ECMAScript 1st Edition (ECMA-262) | Standard | Initial definition. Implemented in JavaScript 1.1. |
ECMAScript 5.1 (ECMA-262) The definition of 'Object.prototype.valueOf' in that specification. | Standard | |
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Object.prototype.valueOf' in that specification. | Standard | |
ECMAScript Latest Draft (ECMA-262) The definition of 'Object.prototype.valueOf' in that specification. | Draft |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | Yes | Yes | 1 | Yes | Yes | Yes |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | Yes | Yes | Yes | 4 | Yes | Yes | Yes |
Server | |
---|---|
Node.js | |
Basic support | Yes |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/valueOf