mozilla

Revision 494473 of Object.prototype.valueOf()

  • Revision slug: Web/JavaScript/Reference/Global_Objects/Object/valueOf
  • Revision title: Object.prototype.valueOf()
  • Revision id: 494473
  • Created:
  • Creator: fscholz
  • Is current revision? No
  • Comment

Revision Content

{{JSRef("Global_Objects", "Object")}}

Summary

The valueOf() method returns the primitive value of the specified object.

Syntax

object.valueOf()

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 {{jsxref("Object.toString", "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.

Examples

Example: Using valueOf

o = new Object();
myVar = o.valueOf();      // [object Object]

Specifications

Specification Status Comment
ECMAScript 1st Edition. Implemented in JavaScript 1.1 Standard Initial definition.
{{SpecName('ES5.1', '#sec-15.2.4.4', 'Object.prototype.valueOf')}} {{Spec2('ES5.1')}}  
{{SpecName('ES6', '#sec-object.prototype.valueof', 'Object.prototype.valueOf')}} {{Spec2('ES6')}}  

Browser compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }}

See also

  • {{jsxref("Object.prototype.toString()")}}
  • {{jsxref("Global_Objects/parseInt", "parseInt")}}

Revision Source

<div>
 {{JSRef("Global_Objects", "Object")}}</div>
<h2 id="Summary" name="Summary">Summary</h2>
<p>The <code><strong>valueOf()</strong></code> method returns the primitive value of the specified object.</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
<code><var>object</var>.valueOf()</code></pre>
<h2 id="Description" name="Description">Description</h2>
<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 class="brush: js">
[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>
<h3 id="Overriding_valueOf_for_custom_objects">Overriding <code>valueOf</code> for custom objects</h3>
<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 class="brush: js">
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 class="brush: js">
myNumber.valueOf()</pre>
<div class="note">
 <p><strong>Note: </strong>Objects in string contexts convert via the {{jsxref("Object.toString", "toString()")}} 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>[object <em> type</em> ]</code>". But many objects do not convert to number, boolean, or function.</p>
</div>
<h2 id="Examples" name="Examples">Examples</h2>
<h3 id="Example:_Using_valueOf" name="Example:_Using_valueOf">Example: Using <code>valueOf</code></h3>
<pre class="brush: js">
o = new Object();
myVar = o.valueOf();      // [object Object]
</pre>
<h2 id="Specifications">Specifications</h2>
<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>ECMAScript 1st Edition. Implemented in JavaScript 1.1</td>
   <td>Standard</td>
   <td>Initial definition.</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-15.2.4.4', 'Object.prototype.valueOf')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-object.prototype.valueof', 'Object.prototype.valueOf')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{ CompatibilityTable() }}</p>
<div id="compat-desktop">
 <table class="compat-table">
  <tbody>
   <tr>
    <th>Feature</th>
    <th>Chrome</th>
    <th>Firefox (Gecko)</th>
    <th>Internet Explorer</th>
    <th>Opera</th>
    <th>Safari</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
   </tr>
  </tbody>
 </table>
</div>
<div id="compat-mobile">
 <table class="compat-table">
  <tbody>
   <tr>
    <th>Feature</th>
    <th>Android</th>
    <th>Chrome for Android</th>
    <th>Firefox Mobile (Gecko)</th>
    <th>IE Mobile</th>
    <th>Opera Mobile</th>
    <th>Safari Mobile</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
   </tr>
  </tbody>
 </table>
</div>
<h2 id="See_Also" name="See_Also">See also</h2>
<ul>
 <li>{{jsxref("Object.prototype.toString()")}}</li>
 <li>{{jsxref("Global_Objects/parseInt", "parseInt")}}</li>
</ul>
Revert to this revision