If the Number() function was generic in nature then the parseInt() and parseFloat() functions will act to be more specific in what they do.
parseInt() will parse the data into an integer value(if possible) and return the result. It returns ‘NaN’ i.e. ‘Not a Number’ if the data is unparseable.
Similarly, parseFloat() parses float values.
Here are a few examples to help you understand these two functions better:
var a = “10″;
alert(parseInt(a));
alert(parseFloat(a));
output:
10
10
a = “10.12″;
alert(parseInt(a));
alert(parseFloat(a));
output:
10
10.12
a = ” 10.25 as you can see, white spaces are allowed and also alphabets after the number”
alert(parseInt(a));
alert(parseFloat(a));
output:
10
10.25
a = ” nope this is wrong, wont work 100.24″;
alert(parseInt(a));
alert(parseFloat(a));
output:
NaN
NaN