Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

JavaScript Number valueOf( ) Method

  • Last Updated : 04 Sep, 2020

Below is the example of the Number valueOf( ) Method.

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

  • Example:




            
    <script type="text/javascript"
      var num=NaN; 
      document.write("Output : " + num.valueOf());             
    </script
  • OUTPUT:
    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
  1. 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
  2. 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
  3. 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
  4. 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

Supported Browsers:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Apple Safari
  • Opera



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!