Below is the JavaScript code snippet I have written.
function bar() {
var x = "Amy";
x = parseInt(x);
console.log(x);
if (isNaN(x)) {
console.log("Your entry is not a number");
} else {
if (typeof (x) === "number") {
console.log("number");
} else if (typeof (x) === "string") {
console.log("string");
}
}
}
When trying to parse an integer from a string, NaN was returned as the value.
It is known that typeof(NaN) returns "number" and numbers can be compared directly in an IF condition. However, this approach did not work in this case.
After attempting isNaN(x), it successfully identified when x was not a number, while x!=NaN failed to do so.
Why is this? Any insights?