Muhammad Juma'a Muhammad Juma'a - 1 year ago 121
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.

Answer Source

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))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download