What is the number valueOf() in Javascript?

The valueOf() is a number method we can use to return the primitive value of a number. The primitive value is the value of type undefined, null, boolean, number, string, or symbol.

Note: The valueOf() method is used internally by Javascript. It is not used explicitly in web code.

Declaration

The valueOf() method is declared as follows:

num.valueOf()
  • num: The number whose primitive value is returned.

Note: This method does not accept any parameters.

Return value

The valueOf() method returns a number which is the primitive value of num.

Browser compatibility

The valueOf() method is supported by the following browsers:

Code

Consider the code snippet below, which demonstrates the use of the valueOf() method:

var num = 15.99
console.log("Value of 15.99:",num.valueOf());
num = 1/0
console.log("Value of 1/0:",num.valueOf());
num = 0/0
console.log("Value of 0/0:",num.valueOf());
num = 0
console.log("Value of 0:",num.valueOf());
num = -15.99
console.log("Value of -15.99:",num.valueOf());

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved