Number valueOf( ) Method In JavaScript

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. 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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    OUTPUT:

    Output : 213
  2. Passing a negative number as an argument in the valueOf() method.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    OUTPUT:

    Output : -213
  3. Passing a zero as an argument in the valueOf() method.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    OUTPUT:

    Output : 0
  4. Passing an equation(equating to infinite value) as an argument in the valueOf() method.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    OUTPUT:

    Output : Nan


My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.