Open In App

What is valueOf() Method in JavaScript ?

In JavaScript, the valueOf() method is a method available on most built-in objects, including strings, numbers, booleans, dates, and more. It returns the primitive value of the specified object.

For instance, if you have a number object created using new Number(42), calling valueOf() on it will return the primitive value 42, which is the underlying numeric data represented by the object.



Syntax:

object.valueOf()

Example: Here, the valueOf() method is called on the num object, which is an instance of the Number object created using the new operator. The method returns the primitive value of the num object, which is the numeric value 42. This value is then assigned to the variable primitiveValue.




const num = new Number(42);
const primitiveValue = num.valueOf();
 
console.log(primitiveValue); // Output: 42

Output

42
Article Tags :