Table of Content
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.
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
If you are a coder, one of your primary requirements is to have a trustworthy code- editor. A Code editor…
JavaScript Math.sqrt() solves the need to get square roots fast. Before it, you wrote custom code or used loops. It…
JavaScript has a statement called "with" that changes how code accesses object properties. The"with" Function makes a temporary scope, so…
The "do while" loop in JavaScript runs code once before checking the condition. Use it when the code must run…
If you are ready to take your first steps into the realm of web development, then there cannot be a…
String Operators in JavaScript help you work with text. You can join, build, or change strings. Most of what you…
Click a button on a website. Something changes. A menu opens or a message pops up. That’s how JavaScript works.…
Math.atan() function in JavaScript helps you to solve math problems in your code. It turns a number into an angle.…
JavaScript Math.log() was added to help people find the natural log of a number. It solves real problems in science…
You may need to loop through properties when you work with objects in JavaScript. The for...in loop helps you do…