Converting to the Number Type
Explore how to convert non-numeric values to numbers in JavaScript. Understand the differences and rules for Number, parseInt, and parseFloat functions to handle various formats and types. Gain practical examples to apply these conversions in web development.
JavaScript provides three functions to convert non-numeric values into numbers:
- the
Number()casting function - the
parseInt()function - the
parseFloat()function
While
Number()is used to convert any data to numbers, parseInt() and parseFloat() are used specifically to convert strings to numbers.
The first function that can be used to convert non-numeric values into numbers is the Number() casting function.
The Number() function
Rules of the Number() Function
The Number() function uses these rules:
-
An undefined value returns
NaN. -
null is converted to 0.
-
When the argument is a number, it ...