You will learn what the JavaScript valueOf function does and how it works. It returns the primitive value of an object without extra steps or complex calls.
Table of Content
Understand the valueOf Function in JavaScript
The valueOf function returns the primitive value of an object. JavaScript calls this function on objects during type conversion.
Here is an example:
object.valueOf()This syntax calls valueOf on any object. It gives you the direct primitive value of that object.
JavaScript calls valueOf automatically in expressions. It also calls it when you convert an object into a number or string. This lets objects act like primitive values in normal operations.
Here is an example:
let num = new Number(42);
let result = num.valueOf();
console.log(result); This returns the primitive number 42. The result is easy to interpret because JavaScript extracts the raw value.
You use valueOf to get the primitive form of a wrapped object. This prevents unwanted behavior during arithmetic or string operations.
Custom valueOf Method Inside Objects
You can define your own valueOf method inside an object. This lets that object return a custom primitive value.
Here is an example:
let price = {
amount: 300,
valueOf: function() {
return this.amount;
}
};
console.log(price + 50);This shows it in action clearly. JavaScript uses the custom valueOf and returns 350 as the result.
The Difference Between valueOf and toString Methods
The toString method returns a string that represents the object. The valueOf method returns the primitive value of the object.
Here is a table that shows you key differences:
| Aspect | valueOf | toString |
|---|---|---|
| Return type | Primitive value | String representation |
| Default behavior | Called during operations | Called in string context |
| Custom override | Returns custom primitive | Returns custom string |
Examples
Number Object:
let num = new Number(100);
console.log(num.valueOf());This returns the primitive number 100 because valueOf extracts the inner value of num.
Custom Date Object:
let date = new Date();
console.log(date.valueOf());This returns the timestamp in milliseconds since January 1, 1970. The output lets you work with the date as a number.
Custom valueOf in a Class:
class Score {
constructor(points) {
this.points = points;
}
valueOf() {
return this.points;
}
}
let s = new Score(50);
console.log(s + 10);This shows it in action clearly.
The result is 60 because JavaScript calls the custom valueOf on the Score instance.
Compare valueOf and toString:
let obj = {
valueOf: function() {
return 7;
},
toString: function() {
return 'seven';
}
};
console.log(obj + 3);
console.log(String(obj));The first call returns 10 because valueOf runs during addition.
The second call returns ‘seven’ because toString runs during string conversion.
Wrapping Up
You learned how valueOf returns primitive values and how to define custom behavior.
You also saw the difference between valueOf and toString with examples.
Here is a quick recap:
- valueOf returns the primitive value of an object.
- You can override valueOf to define custom primitive behavior.
- toString returns a string version of the object.
FAQs
What is JavaScript valueOf function?
valueOf() function in JavaScript returns the primitive value of an object.
For example:
let num = new Number(42);
console.log(num.valueOf()); // 42
This method is often called automatically in operations.
How does valueOf work with JavaScript Date objects?
Date object returns the timestamp in milliseconds when valueOf() is used.
let d = new Date("2025-09-09");
console.log(d.valueOf());
// Example Output: 1757376000000
- The number represents milliseconds since Jan 1, 1970.
- It is often used in comparisons.
What is the difference between valueOf and toString in JavaScript?
valueOf() returns a primitive value.
toString() returns a string representation of the object.
let num = new Number(10);
console.log(num.valueOf()); // 10
console.log(num.toString()); // "10"
- Use
valueOf()for arithmetic logic. - Use
toString()for readable output.
Can you override valueOf function in custom JavaScript objects?
valueOf() method.
let obj = {
value: 200,
valueOf: function() {
return this.value * 2;
}
};
console.log(obj + 50); // 450
The custom method changes how the object behaves in operations.Similar Reads
JavaScript toReversed returns a new array with the reversed order of its elements. It does not change the original array…
The this keyword links code, context, and object reference in JavaScript. You can use it to refer to the owner…
The Math.acos function finds the angle in radians from a cosine value in JavaScript. It gives a value between 0…
JavaScript Math.log() was added to help people find the natural log of a number. It solves real problems in science…
You use loops in JavaScript to repeat code without copying it many times. This helps when you handle lots of…
In this guide, you will learn how JavaScript functions work and why they matter. Each section shows what you need…
The toString function in JavaScript converts a value to a string form without change. It works on numbers, arrays, and…
JavaScript Symbol Type gives a way to make unique values. A Symbol never matches any other value and this helps…
JavaScript arithmetic operators let you add, subtract, multiply, or divide numbers. You can also use them to find remainders or…
If you are a coder, one of your primary requirements is to have a trustworthy code- editor. A Code editor…