-4

The below code is just to display "true" or "false" based on whether the input to isNaN() function is a number or not. In isNaN() function, I am converting the type of number to string using the toString() function. But still, the output I am getting is 'false' instead of 'true'

<html>

  <head>
    <title>check</title>

            <script type='text/javascript'>         

            function checkRun(){
                var obj = {
                 atr : 1,
                 prof : 'dtc'
                }           
                alert(isNaN(obj.atr.toString()));
            }

            </script>

        </title>
    </head>

    <body>    
        <input type='text' name='checkName' id='check1' value='val1' class='class1'/><br><br>
        <button type='button' name='checkName' id='check3' value='val3' class='class3' onclick='checkRun()'>Hello</button>
    </body>

</html>
p-a-o-l-o
  • 9,807
  • 2
  • 22
  • 35
Sami Kh
  • 117
  • 2
  • 14
  • 2
    You might want to use [Number.isInteger()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isInteger) – Aaron Jan 16 '18 at 10:25
  • isNan (Not-a-Number) will check if the value is not an integer. It will return TRUE if it equates to Nan, otherwise, it will return false. What you did is you converted your object to string. Obviously, checking it with isNan will return FALSE. – Tibs Jan 16 '18 at 10:25
  • 2
    It's all [in the docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN#Description) – Adelin Jan 16 '18 at 10:26
  • @Tibs pls avoid answering questions in the comments – EKW Jan 16 '18 at 10:28
  • you are passing number and check that is not a number please check what are you asking – Sourabh Somani Jan 16 '18 at 10:29
  • @EKW did you just downvote my answer because it wasn't yours and told Tibs not to help by commenting? – Tomasz Bubała Jan 16 '18 at 10:29
  • @SourabhSomani But I changed the type of obj.atr to string by appending toString() function. Please check. – Sami Kh Jan 16 '18 at 10:32
  • @TomaszBubała I downvoted your answer because, as I commented, your answer doesn't answer the question. – EKW Jan 16 '18 at 10:32
  • It did adress the question. I've shown why "1" as string is a Number. You didn't see that he converted it to String. Make sure you know what you're talking about before downvoting people. – Tomasz Bubała Jan 16 '18 at 10:35
  • You can use typeof if you just want to check what datatype your variable is. Read: https://stackoverflow.com/questions/4059147/check-if-a-variable-is-a-string-in-javascript – Tibs Jan 16 '18 at 10:40

3 Answers3

3

obj.atr is 1, which is a number. isNaN returns true if its input is "not a number". Another way to think of it is that isNaN returns false if its input is a number. What you are experiencing is the expected behavior.

Another thing to keep in mind: isNaN checks if the value can be interpreted as a number, not that it is stored as a number. isNaN("1234") will return false because "1234"(the string) can be converted into 1234(the number)

If you want to check if the value is actually stored as a number, you can do typeof value === "number"

EKW
  • 2,059
  • 14
  • 24
  • But I changed the type of obj.atr to string by appending toString() function. Please check. – Sami Kh Jan 16 '18 at 10:32
  • @Sami Kh You converted 1 to "1". "1" is being interpreted as 1, and isNaN will return false, because 1 is a number. – Tomasz Bubała Jan 16 '18 at 10:36
  • @Adelin because `Number.isInteger()` only returns true for integers. What if it was a float value? – EKW Jan 16 '18 at 10:44
1

The isNaN function is the %isNaN% intrinsic object. When the isNaN function is called with one argument number, the following steps are taken:

   1. Let num be -> ToNumber(number).  /because of this toString() not consider

   2. If num is NaN, return true.

   3. Otherwise, return false.

NOTE: A reliable way for ECMAScript code to test if a value X is a NaN is an expression of the form X !== X. The result will be true if and only if X is a NaN.check Here

SF..MJ
  • 862
  • 8
  • 19
0

There are two isNaN functions.

The global isNaN function, converts the tested value to a Number, then tests it. That is why even though you had converted obj.atr to a string, it was converted back to a number and hence returned false.

Number.isNaN does not convert the values to a Number, and will not return true for any value that is not of the type Number. So, it will also return false as the type of obj.atr is string.

From what you had asked in the question, I think you can go for typeof val === number as suggested by @EKW

Varun Sharma
  • 1,602
  • 1
  • 13
  • 37
  • Assuming Number.isNaN doesnt count strings as numbers wouldn't Number.isNaN(string) return true? – EKW Jan 16 '18 at 13:35
  • No. It won't. As per MDN docs "The Number.isNaN() method determines whether the passed value is NaN and its type is Number. It is a more robust version of the original, global isNaN()." Hence strings will also return false – Varun Sharma Jan 16 '18 at 13:38