Javascript | Number() Function
Number() is an in built javascript function which is used to convert data type to number.
Syntax:
Number(query javascript variable)
Parameters: The parameter of the number function is the javascript variable which is user input to be converted.The number function converts any type of javascript variable to number type.
Return Values:
Number() function returns the number format for any type of javascript variable.
Examples:
Input : Number(true);
Number(false);
Output : 1
0
Input : Number("10 20");
Output : NaN
Not a number is returned by the compiler.
Input : Number(new Date("2017-09-30"));
Output : 1506729600000
The Number() method above returns the number of milliseconds since 1.1.1970.
Input : Number("John");
Output : NaN
Below Programs illustrate the given examples more clearly.
<script> // JavaScript to illustrate Number() function function func() { // Original string var a = "True"; var value = Number(a); document.write(value); } // Driver code func(); < /script> |
Output:
1
Program 2:
<script> // JavaScript to illustrate Number() function function func() { // Original string var a = "10 20"; var value = Number(a); document.write(value); } // Driver code func(); |
Output:
NaN
Program 3:
<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
Recommended Posts:
- JavaScript | Number.isFinite() function
- JavaScript | Number.isInteger( ) function
- How to disable scroll to change number in <input type="number"> field using JavaScript/jQuery?
- How to call a function that return another function in JavaScript ?
- JavaScript | Number.MAX_VALUE & Number.MIN_VALUE with Examples
- How to convert a float number to the whole number in JavaScript?
- JavaScript | String() Function
- JavaScript | Math.abs( ) function
- JavaScript | toPrecision( ) Function
- JavaScript | Function Call
- JavaScript | toFixed( ) Function
- How to override a JavaScript function ?
- JavaScript | Math.pow( ) Function
- JavaScript | Math.E() function
- JavaScript | Function binding
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.

