concider the following code:
var postValue = "99"; var number = new Number(postValue); //also tried var number = parseInt(postValue); if (isNaN(number)) { alert('please enter a number'); } How come isNan(number) in both cases is false?
concider the following code:
var postValue = "99"; var number = new Number(postValue); //also tried var number = parseInt(postValue); if (isNaN(number)) { alert('please enter a number'); } How come isNan(number) in both cases is false?
parseInt() will convert your string to an integer. To do so, it reads characters from the left of the string to find as many number characters as it can, then returns that number.
In this case, since "99" is all numeric characters, the result is the number 99. Which is very clearly "not-not a number" and therefore isNaN returns false.
isNaN will return true if the input string starts with something that isn't a digit.