The Wayback Machine - http://web.archive.org/web/20211216005813/https://www.geeksforgeeks.org/javascript-number-function/amp/

Javascript Number() Function

Below is the example of the Number() function.

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!

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:

More example codes for the above function are as follows:

Program 1:




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

Output:

NaN

Program 2:




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

Output:

1506729600000

Program 4:




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

Output:

NaN

Supported Browsers:




Article Tags :