Converting Variables to Numbers
There are 3 JavaScript methods that can be used to convert variables to numbers:
- The
Number()method - The
parseInt()method - The
parseFloat()methodThese methods are not number methods, but global JavaScript methods.
Global JavaScript Methods
JavaScript global methods can be used on all JavaScript data types.
These are the most relevant methods, when working with numbers :Number() : Returns a number, converted from its argument.
parseFloat() : Parses its argument and returns a floating point number.
parseInt() : Parses its argument and returns an integer .
try it in the console :
Number(10.34) ; // returns 10.34
parseFloat(2.56); // returns 2.56
parseInt(2.56); // returns 2
in all above three methods :
If the number cannot be converted,
NaN(Not a Number) is returned." NaNis a JavaScript reserved word indicating that a number is not a legal number. "

Comments
Post a Comment