Converting to the Number Type
In this lesson we shall see how different JavaScript types can be converted to the number type. Let's begin!
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. ...