Skip to content
Related Articles
Open in App
Not now

Related Articles

JavaScript Number valueOf( ) Method

Improve Article
Save Article
  • Last Updated : 05 Dec, 2022
Improve Article
Save Article

The 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()

Return Value: The valueof() method in JavaScript returns a number representing the primitive value of the specified Number object. 

Primitive Value: Primitive values are the type of values that a variable can hold. A primitive value is stored directly in the location that the variable accesses. Primitive values are data that are stored on the stack. Primitive types include Undefined, Null, Boolean, Number, or String.

Below is an example 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




<script type="text/javascript">
    var num=NaN;
    console.log("Output : " + num.valueOf());            
</script>

Output:

NaN

Example 2: Passing a positive number as an argument in the valueOf() method. 

Javascript




<script type="text/javascript">
    var num=213;
    console.log("Output : " + num.valueOf());            
</script>

Output:

 213

Example 3: Passing a negative number as an argument in the valueOf() method. 

Javascript




<script type="text/javascript">
    var num=-213;
    console.log("Output : " + num.valueOf());            
</script>

Output:

-213

Example 4: Passing zero as an argument in the valueOf() method. 

Javascript




<script type="text/javascript">
    var num=0;
    console.log("Output : " + num.valueOf());            
</script>

Output:

0

Example 5: Passing an equation(equating to infinite value) as an argument in the valueOf() method. 

Javascript




<script type="text/javascript">
    var num=0/0;
    console.log("Output : " + num.valueOf());            
</script>

Output:

Nan

We have a complete list of Javascript Number Objects methods, to check those please go through this Javascript Number Complete reference article.

Supported Browsers:

  • Google Chrome 1 and above
  • Internet Explorer 4 and above
  • Firefox 1 and above
  • Apple Safari 1 and above
  • Opera 3 and above

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!