Open In App

What is valueOf() Method in JavaScript ?

Last Updated : 12 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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()
  • object: The object for which to retrieve the primitive value.

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.

Javascript




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


Output

42

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads