JavaScript valueof() Method

Learn via video course
FREE
View all courses
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
Topics Covered

Overview

The valueOf() method in JavaScript returns a primitive value of the specified object on which it is called. JavaScript internally invokes the valueOf() method automatically when an object is encountered whose primitive value is expected. The method accepts no parameter and returns a primitive value.

Syntax of JavaScript valueOf() Method

The declaration of valueOf in JavaScript is as follows:

Here, object is the object whose primitive value is returned from the method. The valueOf in JavaScript does not accept any parameters.

Return Value of JavaScript valueOf() Method

The valueOf() method returns a primitive value of the specified object on which it is called. If the object of type Object has no primitive value, the valueOf() method returns the object itself. For example, if we have a JavaScript object like var obj = {name: 'Rahul', age: 21}, the primitive value returned from the valueOf() method for obj will be this object itself i.e. {name: 'Rahul', age: 21}.

Example

Now that we are familiar with the syntax of the valueOf() in JavaScript. Let us see examples to understand how we can use this method in our codes.

Example 1

Consider the code snippet below, demonstrating the use of the valueOf() method.

Output

In this example, the valueOf() method is not overridden. Because of this, the object itself is returned from the function call.

Example 2

Let us consider an example where we override the valueof in javascript for a custom class.

Output

Here, the valueOf() method of the Student object is overridden such that the id and string are concatenated and returned as the primitive value of the Student object by the valueOf() method.

How Does JavaScript valueOf() Method Work?

The valueOf() function in JavaScript returns the primitive value of the object it is called upon Object.valueOf().

There are six primitive type values in JavaScript as mentioned below:

  • Boolean: false or true
  • String: 'hello world'
  • Number: 32, 42
  • Null
  • Undefined
  • Symbol: Symbol('my_symbol')

The valuof() method returns a value whose typeof is either one of the six mentioned above. JavaScript internally invokes the valueOf() method automatically when an object is encountered whose primitive value is expected. Every descendant from Object by default inherits the valueOf() method. We need to override this method for every built-in object to return the appropriate value. If the object has no primitive value, the valueOf() method returns the object itself.

We can use valueOf within our code to convert a built-in object into primitive values. We can do this by overriding Object.prototype.valueOf() for every custom object we create to call our custom method instead of the default Object method. Also, JavaScript uses the valueOf method for type conversion when comparing values for comparison operators (==, >, <, <=, >=).

Overriding the valueOf() Method

We can override the valueOf() method of any custom object by overriding Object.prototype.valueOf() for that object. This can be done as follows:

Here, object is the object whose primitive value we wish to return, and primitive_value is the custom primitive value we want to return from the valueOf method for the specified object.

More Examples

A. JavaScript Number valueOf()

Below is an example of the Number valueOf() method.

Output

Here, the valueOf() method in JavaScript is used to return the primitive value of a number.

B. JavaScript String valueOf()

Let us see the code snippet that shows the use of the valueOf method on Strings.

Output

In this example, we create a string object test_string with the value hello world. When the valueOf() method is called on test_string, it returns a string (primitive type) as observed from the code snippets output.

C. Javascript Object.valueOf()

Let us try to use the valueOf() method on a custom-created object.

Output

Here, the valueof in javascript returns the object itself because we have not overridden the valueOf method for this object. This can be resolved by overriding the valueOf() for the Car class, as shown in the code snippet below.

Output

Supported Browser

Most browser today has support for the valueOf() method. Here is the list of browsers that support the title tag.

  • Google Chrome (>=1)
  • Firefox (>=1)
  • Opera (>=3)
  • Safari (>=1)
  • Internet Explorer (>=4)

Conclusion

  • The valueOf function in JavaScript returns the primitive value of the object it is called upon Object.valueOf().
  • JavaScript has six primitive types: Number, Boolean, String, Symbol, Null, and Undefined.
  • JavaScript internally invokes the valueOf() method automatically when an object whose primitive value is expected is encountered.
  • The valueOf() method can convert the built-in objects into primitive values by overriding Object.prototype.valueOf() for the object instead of using the default Object method.