Muhammad Juma'a Muhammad Juma'a - 5 months ago 62
Javascript Question

Number between double quotes

According to this code

function sayHi(myAge) {
"use strict";
if (isNaN(myAge)) {
return "Ture";
} else {
return "False";

isNan() return false, Why? "12" is not a number.

Because When I do this

var myAge = "12";
alert(myAge === 12);

it will return false, because "12" is a string but 12 a number.


Because NaN is a special value in JS, not a type. sayHi(NaN) will return true.

If you want to check if the value is the Number type, you should do

if (typeof myAge === "number")

And if you want to be sure, that it's not NaN as well, then

if (typeof myAge === "number" && !isNaN(myAge))