JavaScript Number valueOf( ) Method

22 Dec, 2021
Below is the example of the Number valueOf( ) Method.
Example:        <script type="text/javascript">   var num=NaN;   document.write("Output : " + num.valueOf());             </script> 
        <script type="text/javascript">   var num=NaN;   document.write("Output : " + num.valueOf());             </script> 
OUTPUT:Output : NaN
Output : NaN
The 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.
What is 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.
Examples:
Input :  213
Output : 213
     
Input : -213
Output :-213

Input :  0
Output : 0

Input :  0/0
Output : NaN

Passing a positive number as an argument in the valueOf() method.<script type="text/javascript">  var num=213;  document.write("Output : " + num.valueOf());            </script>OUTPUT:Output : 213Passing a negative number as an argument in the valueOf() method.<script type="text/javascript">  var num=-213;  document.write("Output : " + num.valueOf());            </script>OUTPUT:Output : -213Passing a zero as an argument in the valueOf() method.<script type="text/javascript">  var num=0;  document.write("Output : " + num.valueOf());            </script>OUTPUT:Output : 0Passing an equation(equating to infinite value) as an argument in the valueOf() method.<script type="text/javascript">  var num=0/0;  document.write("Output : " + num.valueOf());            </script>OUTPUT:Output : Nan
Passing a positive number as an argument in the valueOf() method.<script type="text/javascript">  var num=213;  document.write("Output : " + num.valueOf());            </script>OUTPUT:Output : 213
<script type="text/javascript">  var num=213;  document.write("Output : " + num.valueOf());            </script>
OUTPUT:
Output : 213
Passing a negative number as an argument in the valueOf() method.<script type="text/javascript">  var num=-213;  document.write("Output : " + num.valueOf());            </script>OUTPUT:Output : -213
<script type="text/javascript">  var num=-213;  document.write("Output : " + num.valueOf());            </script>
OUTPUT:
Output : -213
Passing a zero as an argument in the valueOf() method.<script type="text/javascript">  var num=0;  document.write("Output : " + num.valueOf());            </script>OUTPUT:Output : 0
<script type="text/javascript">  var num=0;  document.write("Output : " + num.valueOf());            </script>
OUTPUT:
Output : 0
Passing an equation(equating to infinite value) as an argument in the valueOf() method.<script type="text/javascript">  var num=0/0;  document.write("Output : " + num.valueOf());            </script>OUTPUT:Output : Nan
<script type="text/javascript">  var num=0/0;  document.write("Output : " + num.valueOf());            </script>
OUTPUT:
Output : Nan
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
ysachin2314
javascript-math
JavaScript-Numbers
JavaScript
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
Remove elements from a JavaScript Array
How to append HTML code to a div using JavaScript ?
Difference Between PUT and PATCH Request
How to Open URL in New Tab using JavaScript ?
How to get character array from string in JavaScript?
Roadmap to Learn JavaScript For Beginners
How do you run JavaScript script through the Terminal?
Node.js | fs.writeFileSync() Method