JavaScript Number valueOf( ) method in JavaScript is used to return the primitive value of a number. This method is usually called internally by JavaScript and not explicitly in web code
Syntax:
number.valueOf()
Parameters: This method does not accept any parameter.
Return Value: The valueof() method in JavaScript returns a number representing the primitive value of the specified Number object.
Below are examples of the Number valueOf( ) Method:
Example 1: In this example, the output comes out to be NaN as the value stored in the variable is NaN.
Javascript
let num=NaN; console.log(num.valueOf()); |
Output:
NaN
Example 2: Passing a positive number as an argument in the valueOf() method.
Javascript
let num=213; console.log(num.valueOf()); |
Output:
213
Example 3: Passing a negative number as an argument in the valueOf() method.
Javascript
let num=-213; console.log(num.valueOf()); |
Output:
-213
Example 4: Passing zero as an argument in the valueOf() method.
Javascript
let num=0; console.log(num.valueOf()); |
Output:
0
Example 5: Passing an equation(equating to infinite value) as an argument in the valueOf() method.
Javascript
let num=0/0; console.log(num.valueOf()); |
Output:
Nan
Supported Browsers:
- Edge
- Firefox
- Apple Safari
- Opera
We have a complete list of Javascript Number Objects methods, to check those please go through this Javascript Number Complete reference article.