javascript - parseInt() check for NaN isn't working properly -
this question has answer here:
- how check number nan in javascript? 27 answers
i working on jquery plugin , need make sure value number, or string number. made following code, val variable may or may not number.
if (typeof val !== 'number') {//if "val" isn't number if (typeof val === 'string' && parseint(val) !== nan) {//see if "val" string number val = parseint(val); } } but reason, while testing, if val string text, still returns parseint(val) !== nan true. later tested chrome console, , got:
as can see, 'asf' not-a-number. wherever returns true, should return false, , wherever returns false, should return true. can see tested make sure nan isn't string.
above can see return values same, time correct.
am missing something? or there different way supposed this?
nan not can compare to. need use :
isnan(parseint('asf')) // true
isnan(parseint('123')) // false


Comments
Post a Comment