w3resource

JavaScript: Number function

Description

The number function is used to converts a specified object to a number.

Syntax

Number(obj)

Parameters

obj: Required. An object.

Note: If the object is a Date object the function returns a value in milliseconds calculated from 01 January, 1970 UTC (GMT). The return value is positive if the given date is after the said date and negative if before the said date.

If the object is a string and if there is no well-formed numeric literal the function returns NaN.

Example of Number() function

The following example demonstrates how to use Number() function.

JavaScript Code:

var str1 = "4400.23";
var num1 = Number(str1);
console.log('"4400.23" converted to : '+num1); 
    

View the example in the browser

Live demo:

See the Pen number-function-1 by w3resource (@w3resource) on CodePen.


Previous: JavaScript: NaN
Next: JavaScript: String function

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/javascript/functions/number-function.php