javascriptfunctionnantypeof

why does it always executing the else block of code?


I was trying to make an isNaN() function that checks the user's input if its a NaN or not, but it always skips the the first condition whatever the user's input is.

function isNaN() {
    let value = prompt("enter value");

    if (typeof value === "number") {
    console.log("value is number");
    }else {
    console.log("NaN");
    }
} isNaN();

I made the program to checks the user's input data type by typeof operator.


Solution

  • Try this:

        let value = prompt("enter value");
        console.log("Value entered:" + value);
        console.log("Type of value: " + typeof value);
    
        if (!isNaN(parseFloat(value))) {
        console.log("String contains a number");
        }else {
        console.log("NaN");
        }