The Number() method is applicable for converting JavaScript variables into numbers.
Example
Number(true); Number(false); Number(“10”); Number(” 10″); Number(“10 “); Number(” 10 “); Number(“10.33”); Number(“10,33”); Number(“10 33”); Number(“John”); |
If the number cannot be converted, NaN (Not a Number) will be returned. |
Number() can also be used to convert a date into a number.
Example
Number(new Date(“1970-01-01”)) |
Note: The Date() method returns the number of milliseconds elapsed since January 1, 1970. |
There are 86,400,000 milliseconds between January 2, 1970, and January 1, 1970.
Example
Number(new Date(“1970-01-02”)) |
Example
Number(new Date(“2017-09-30”)) |
Example
parseInt(“-10”); parseInt(“-10.33”); parseInt(“10”); parseInt(“10.33”); parseInt(“10 20 30”); parseInt(“10 years”); parseInt(“years 10”); |
If the string cannot be converted, NaN (Not a Number) is returned.
parseFloat() parses a string and returns a floating-point number, allowing spaces, and extracts only the initial number encountered.
Example
parseFloat(“10”); parseFloat(“10.33”); parseFloat(“10 20 30”); parseFloat(“10 years”); parseFloat(“years 10”); |
When the string cannot be converted, parseFloat() returns NaN (Not a Number).
These methods are associated with the Number object.
Method |
Description |
Number.isInteger() |
Returns true if the argument passed is an integer |
Number.isSafeInteger() |
Returns true if the provided argument is a safe integer. |
Number.parseFloat() |
Converts a string into a number |
Number.parseInt() |
Translates a string into a numerical value. |
Number Methods Cannot be Used on Variables The aforementioned number methods are part of the JavaScript Number Object. They can solely be accessed in the format Number.isInteger(). Attempting to use X.isInteger() where X is a variable will lead to an error: “TypeError: X.isInteger is not a function.” |
If the argument passed to it is an integer, the Number.isInteger() method returns true.
Example
Number.isInteger(10); Number.isInteger(10.5); |
A safe integer is one that can be precisely represented as a double-precision number.
The Number.isSafeInteger() method returns true if the provided argument is a safe integer.
Example
Number.isSafeInteger(10); Number.isSafeInteger(12345678901234567890); |
Safe integers encompass all integers ranging from -(253 – 1) to +(253 – 1).
For instance, 9007199254740991 falls within this range and is considered safe, while 9007199254740992 exceeds it and is not considered safe.
Number.parseFloat() parses a string and yields a numerical value, permitting spaces, and retrieves only the initial number encountered.
Example
Number.parseFloat(“10”); Number.parseFloat(“10.33”); Number.parseFloat(“10 20 30”); Number.parseFloat(“10 years”); Number.parseFloat(“years 10”); |
When the conversion is not possible, parseFloat() returns NaN (Not a Number).
Note: The Number methods Number.parseInt() and Number.parseFloat() mirror the functionality of the global methods parseInt() and parseFloat(). This modular approach facilitates the use of JavaScript code beyond the browser environment. |
Number.parseInt() parses a string and returns an integer, permitting spaces, and retrieves only the initial number encountered.
Example
Number.parseInt(“-10”); Number.parseInt(“-10.33”); Number.parseInt(“10”); Number.parseInt(“10.33”); Number.parseInt(“10 20 30”); Number.parseInt(“10 years”); Number.parseInt(“years 10”); |
When conversion is not possible, parseInt() returns NaN (Not a Number).