I thought '88', '89', 'blabla' are strings. Was I wrong?


#1

var isEven = function(number) {
    if (number % 2 === 0) {
        return true;  
    } else if (isNaN(number)) {
        return 'This is not a number!';
    } else {
        return false;
    }
}
isEven('89');

return false (and if isEven(89) )

if
isEven('88');
return true (and if isEven(88) )

if
isEven('blabla');
return 'This is not a number!'

I thought '88', '89', 'blabla' are strings. Was I wrong? Please help me to understand this moment.


#2

When we pass any value to isNan(), It first coerced into a number If it can be coerced then the resulitng value is checked if its a number of not.

as "67" coerced into 67..
isNan() sees it as a number and returns false.

Check these Articles..

isNan() confusion

also check this good article on Coercion
Tutsplus - Coercion


#3

Thank you! But if I don't want isNan() coerced "67" into 67.. What can I do? Nothing? It's a really strange situation...


#4

Yes,Its one of the aspect of the language introduced by designer which is confusing.
I'd suggest to go with MDN documentation.(Its updated frequently and there are tons of examples)


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.