Related Articles

Related Articles

Javascript Number() Function
  • Last Updated : 06 Jul, 2020

Below is the example of the Number() function.

  • Example:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script>
        // JavaScript to illustrate
        // Number() function
        function func() {
      
            // Original string
            var a = true;
      
            var value = Number(a);
            document.write(value);
        }
    // Driver code
    func();
    </script> 

    chevron_right

    
    

  • Output:
    1

The Number() function is used to convert data type to number.

Syntax:

Number(object)

Parameters: This function accept a single parameter as mentioned above and described below:

object: This parameter holds the objects that will be converted any type of javascript variable to number type.



Return Values: Number() function returns the number format for any type of javascript variable.

Below examples illustrate the Number() function in JavaScript:

  • Example 1:
    Input : Number(true);
                 Number(false);
    Output : 1
             0
  • Example 2: Not a number is returned by the compiler.
    Input : Number("10 20");
    Output: NaN
  • Example 3: The Number() method above returns the number of milliseconds since 1.1.1970.
    Input : Number(new Date("2017-09-30"));
    Output : 1506729600000
  • Example 4:
    Input : Number("John");
    Output : NaN
    

More example codes for the above function are as follows:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
    // JavaScript to illustrate Number() function
    function func() {
  
        // Original string
        var a = "10 20";
  
        var value = Number(a);
        document.write(value);
    }
// Driver code
func();
</script>

chevron_right


Output:

NaN

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
    // JavaScript to illustrate Number() function
    function func() {
        var value = Number(new Date("2017-09-30"));
        document.write(value);
    }
// Driver code
func();
</script>

chevron_right


Output:

1506729600000

Program 4:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
    // JavaScript to illustrate Number() function
    function func() {
        var value = Number("John");
        document.write(value);
    }
// Driver code
func();
</script>

chevron_right


Output:

NaN

Supported Browsers:

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

full-stack-img




My Personal Notes arrow_drop_up
Recommended Articles
Page :