• Skip to main content
  • Skip to search
  • Skip to select language
HTML

Structure of content on the web

  • Web APIs

    Interfaces for building web applications

  • Learn
    • CSS

      Learn to style content using CSS

    • Overview

      A customized MDN experience

    • FAQ

      Frequently asked questions about MDN Plus

  • HTTP Observatory

    Scan a website for free

  • JavaScript
  • valueOf()
      • Deutsch
      • Español
      • Français
      • 日本語
      • 한국어
      • Português (do Brasil)
      • Русский
      • 中文 (简体)

    In this article

    • Try it
    • Syntax
    • Description
    • Examples
    • Specifications
    • Browser compatibility
    • See also
    1. assign()
    2. entries()
    3. getOwnPropertyDescriptors()
    4. groupBy()
    5. isFrozen()
    6. seal()
    7. __defineGetter__() Deprecated
    8. __defineSetter__() Deprecated
    9. __lookupGetter__() Deprecated
    10. __lookupSetter__() Deprecated
    11. toLocaleString()
    12. __proto__ Deprecated
    13. bind()
    14. displayName Non-standard
    15. arguments Non-standard Deprecated
    16. caller Non-standard Deprecated
  • Instance methods
    1. __defineGetter__() Deprecated
    2. __defineSetter__() Deprecated
    3. __lookupGetter__() Deprecated
    4. __lookupSetter__() Deprecated
    5. toLocaleString()
    6. __proto__ Deprecated
    7. to an object. This method is meant to be overridden by derived objects for custom type 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

    The this value, converted to an object.

    Note: In order for valueOf to be useful during type conversion, it must return a primitive. Because all primitive types have their own valueOf() methods, calling aPrimitiveValue.valueOf() generally does not invoke Object.prototype.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.

    This method is called in priority by Object.prototype.toString() base implementation), so valueOf() is usually not called in this case.

    All objects that inherit from Object.prototype (that is, all except [Symbol.toPrimitive]() method, which allows even more control over the conversion process, and will always be preferred over valueOf or toString for any type conversion.

    Examples

    Using valueOf()

    The base valueOf() method returns the this 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); / true
    
    console.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 default valueOf method. Your function should take no arguments, since it won't be passed any when called during type conversion.

    For example, you can add a valueOf method to your custom class Box.

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

    With the preceding code in place, any time an object of type Box 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); / 579
    console.log(box == 123); / true
    

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

    js
    box.valueOf();
    

    Using unary plus on objects

    [Symbol.toPrimitive](), means calling its valueOf(). However, if the object doesn't have a custom valueOf() method, the base implementation will cause valueOf() to be ignored and the return value of toString() 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

    • Object.prototype.toString()
    • parseInt()
    • Symbol.toPrimitive

    Help improve MDN

    Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

    Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant