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
The current essay is devoted to the basic principles and introduction of JavaScript. This language no longer needs to be…
The ternary operator in JavaScript keeps your code short. It helps you write conditions inside one line. JavaScript lets you…
JavaScript Ninja Code points to ways that help a person write code that runs fast and stays easy to read.…
Math.atan() function in JavaScript helps you to solve math problems in your code. It turns a number into an angle.…
JavaScript arithmetic operators let you add, subtract, multiply, or divide numbers. You can also use them to find remainders or…
JavaScript Math.log() was added to help people find the natural log of a number. It solves real problems in science…
The forEach loop in JavaScript allows you to go through each item in an array. You do not have to…
Object constructors and class constructors in JavaScript create objects in a structured way. Both provide methods to build reusable code.…
Some numbers come from user input and other come from math operations. Sometimes, these numbers need to stay positive. The…
You need to compare values in almost every JavaScript program. This is where comparison operators come in. They check if…