2

Hi guys i know this code is too easy but i'm new in javascript and i'm still learning it . this code gets a value from user then shows an answer but it dosen't matter if value is number or not it just says = its a number .

<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
        <!--Javascript starts-->
    <script>
        var age = prompt("How old are you ?","18");
           if(age == NaN)
           {
               alert("its not a number");
           }else
           {
               alert("its a number");
           }  
    </script>
        <!--End of Javascript-->

    </body>
</html>
Reza
  • 25
  • 3
  • 2
    `age` is a string for being the return of `prompt()`. Use `isNaN()` to achieve what you want. [isNaN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN). – Lain Jul 01 '20 at 06:24
  • 3
    btw, nothing is eqal to [`NaN`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NaN). – Nina Scholz Jul 01 '20 at 06:24

6 Answers6

2

NaN is not equal to any value in javascript, not even to itself.

prompt returns a string and if you want to check if its a numeric string, check if age can be converted into a number using Number().

var age = prompt("How old are you ?","18");

if(age != '0' && !Number(age)) {    // extra check for '0'
    alert("its not a number");
} else {
    alert("its a number");    
}

couple of corner cases that should be kept in mind in code above:

  1. '0' is a falsey value, so if age is '0', this condition !Number(age) will evaluate to true. To prevent this, add an extra check to make sure age is not '0'
  2. empty string '' when coerced to a number evaluates to 0 and hence the condition !Number(age) will correctly evaluate to true whereas isNan(age) will evaluate to false which will lead to the execution of else block.
Yousaf
  • 27,861
  • 6
  • 44
  • 69
1

The input age will be a string. Use isNaN() instead. This will work as you expected.

<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
        <!--Javascript starts-->
    <script>
        var age = prompt("How old are you ?","18");
           if(isNaN(age))
           {
               alert("its not a number");
           }else
           {
               alert("its a number");
           }  
    </script>
        <!--End of Javascript-->

    </body>
</html>
Jerome Li
  • 176
  • 11
1

<!DOCTYPE html>
<html>

<head>
</head>

<body>
  <!--Javascript starts-->
  <script>
    var age = prompt("How old are you ?", "18");
    if (isNaN(age)) {
      alert("its not a number");
    } else {
      alert("its a number");
    }
  </script>
  <!--End of Javascript-->

</body>

</html>

Can you use isNaN()?

Santa
  • 367
  • 2
  • 8
1
(~~age > 0) ? alert('valid') : alert('invalid')
JavaScript
  • 539
  • 2
  • 10
0

You should Learn about isNaN function.

Pawan Bishnoi
  • 1,759
  • 8
  • 19
0

only NaN, nothing is eqal to NaN, include itself.

var num = NaN;
console.log(num === num); // false
Tushar Vaghela
  • 1,203
  • 1
  • 17
  • 25
yoyo
  • 24
  • 3