Object.prototype.valueOf()

  • Revision slug: JavaScript/Reference/Global_Objects/Object/valueOf
  • Revision title: JavaScript/Reference/Global_Objects/Object/valueOf
  • Revision id: 264769
  • Created:
  • Creator: gary.court
  • Is current revision? No
  • Comment 9 words added, 5 words removed

Revision Content

 

Summary

Returns the primitive value of the specified object

Method of Object
Implemented in JavaScript 1.1
ECMAScript Edition ECMAScript 1st Edition

Syntax

valueOf()

Parameters

None.

Description

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, which is displayed as:

[object Object]

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.

Overriding valueOf for custom objects

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:

myNumber.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 "{{ mediawiki.external('object type') }}". But many objects do not convert to number, boolean, or function.

See Also

{{ languages( { "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Objets_globaux/Object/valueOf", "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/Object/valueOf", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/Object/valueOf" } ) }}

Revision Source

<p> </p>
<h3 id="Summary" name="Summary">Summary</h3>
<p>Returns the primitive value of the specified object</p>
<table class="standard-table"> <thead> <tr> <th class="header" colspan="2">Method of <a href="/en/JavaScript/Reference/Global_Objects/Object" title="en/JavaScript/Reference/Global_Objects/Object"><code>Object</code></a></th> </tr> </thead> <tbody> <tr> <td>Implemented in</td> <td>JavaScript 1.1</td> </tr> <tr> <td>ECMAScript Edition</td> <td>ECMAScript 1st Edition</td> </tr> </tbody>
</table>
<h3 id="Syntax" name="Syntax">Syntax</h3>
<p><code>valueOf() </code></p>
<h3 id="Parameters" name="Parameters">Parameters</h3>
<p>None.</p>
<h3 id="Description" name="Description">Description</h3>
<p>JavaScript calls the <code>valueOf</code> method to convert an object to a primitive value. You rarely need to invoke the <code>valueOf</code> method yourself; JavaScript automatically invokes it when encountering an object where a primitive value is expected.</p>
<p>By default, the <code>valueOf</code> method is inherited by every object descended from <code>Object</code>. Every built-in core object overrides this method to return an appropriate value. If an object has no primitive value, <code>valueOf</code> returns the object itself, which is displayed as:</p>
<pre>[object Object]
</pre>
<p>You can use <code>valueOf</code> within your own code to convert a built-in object into a primitive value. When you create a custom object, you can override <code>Object.prototype.valueOf</code> to call a custom method instead of the default <code>Object</code> method.</p>
<h4 id="Overriding_valueOf_for_custom_objects" name="Overriding_valueOf_for_custom_objects">Overriding <code>valueOf</code> for custom objects</h4>
<p>You can create a function to be called in place of the default <code>valueOf</code> method. Your function must take no arguments.</p>
<p>Suppose you have an object type <code>myNumberType</code> and you want to create a <code>valueOf</code> method for it. The following code assigns a user-defined function to the object's <code>valueOf</code> method:</p>
<pre>myNumberType.prototype.valueOf = function () { return customPrimitiveValue; };
</pre>
<p>With the preceding code in place, any time an object of type <code>myNumberType</code> 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.</p>
<p>An object's <code>valueOf</code> method is usually invoked by JavaScript, but you can invoke it yourself as follows:</p>
<pre>myNumber.valueOf()
</pre>
<h4 id="Note" name="Note">Note</h4>
<p>Objects in string contexts convert via the <code><a href="/en/JavaScript/Reference/Global_Objects/Object/toString" title="en/JavaScript/Reference/Global_Objects/Object/toString">toString</a></code> method, which is different from <code>String</code> objects converting to string primitives using <code>valueOf</code>. All objects have a string conversion, if only "<code>{{ mediawiki.external('object <i>type</i>') }}</code>". But many objects do not convert to number, boolean, or function.</p>
<h3 id="See_Also" name="See_Also">See Also</h3>
<ul> <li><a href="/en/JavaScript/Reference/Global_Objects/parseInt" title="en/JavaScript/Reference/Global_Objects/parseInt">parseInt</a></li> <li><a href="/en/JavaScript/Reference/Global_Objects/Object/toString" title="en/JavaScript/Reference/Global_Objects/Object/toString">toString</a></li>
</ul>
<p>{{ languages( { "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Objets_globaux/Object/valueOf", "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/Object/valueOf", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/Object/valueOf" } ) }}</p>
Revert to this revision