MichalB Posted May 8, 2019 Share Posted May 8, 2019 I would like to suggest to change the example in the JavaScript isNaN() Function topic (https://www.w3schools.com/jsref/jsref_isnan.asp) to the one below. I have added parseFloat of every test example. It shows that sometimes isNaN returns false, but parseFloat returns NaN. Interesting might be also treatment of the date. <!DOCTYPE html> <html> <body> <p>The isNaN() function returns true if the value is NaN (Not-a-Number), and false if not.</p> <p>Click the button to check whether a number is an illegal number.</p> <button onclick="myFunction()">Try it</button> <p id="demo"></p> <script> function myFunction() { var res = ""; res = res + isNaN(123) + ": 123 -> " + parseFloat(123) + "<br>"; res = res + isNaN(-1.23) + ": -1.23 -> " + parseFloat(-1.23) + "<br>"; res = res + isNaN(5-2) + ": 5-2 -> " + parseFloat(5-2) + "<br>"; res = res + isNaN(0) + ": 0 -> " + parseFloat(0) + "<br>"; res = res + isNaN('123') + ": '123' -> " + parseFloat('123') + "<br>"; res = res + isNaN('Hello') + ": 'Hello' -> " + parseFloat('Hello') + "<br>"; res = res + isNaN('2005/12/12') + ": '2005/12/12' -> " + parseFloat('2005/12/12') + "<br>"; res = res + isNaN('') + ": '' -> " + parseFloat('') + "<br>"; res = res + isNaN(true) + ": true -> " + parseFloat(true) + "<br>"; res = res + isNaN(undefined) + ": undefined -> " + parseFloat(undefined) + "<br>"; res = res + isNaN(null) + ": null -> " + parseFloat(null) + "<br>"; res = res + isNaN('NaN') + ": 'NaN' -> " + parseFloat('NaN') + "<br>"; res = res + isNaN(NaN) + ": NaN -> " + parseFloat(NaN) + "<br>"; res = res + isNaN(0 / 0) + ": 0 / 0 -> " + parseFloat(0 / 0) + "<br>"; document.getElementById("demo").innerHTML = res; } </script> </body> </html> Link to comment Share on other sites More sharing options...
justsomeguy Posted May 8, 2019 Share Posted May 8, 2019 parseFloat does something different than what isNaN checks for. isNaN converts to a Number, and then checks if that is NaN. Converting to a Number is not the same as using parseFloat: var res = ""; res = res + isNaN(123) + ": 123 -> " + Number(123) + "\n"; res = res + isNaN(-1.23) + ": -1.23 -> " + Number(-1.23) + "\n"; res = res + isNaN(5-2) + ": 5-2 -> " + Number(5-2) + "\n"; res = res + isNaN(0) + ": 0 -> " + Number(0) + "\n"; res = res + isNaN('123') + ": '123' -> " + Number('123') + "\n"; res = res + isNaN('Hello') + ": 'Hello' -> " + Number('Hello') + "\n"; res = res + isNaN('2005/12/12') + ": '2005/12/12' -> " + Number('2005/12/12') + "\n"; res = res + isNaN('') + ": '' -> " + Number('') + "\n"; res = res + isNaN(true) + ": true -> " + Number(true) + "\n"; res = res + isNaN(undefined) + ": undefined -> " + Number(undefined) + "\n"; res = res + isNaN(null) + ": null -> " + Number(null) + "\n"; res = res + isNaN('NaN') + ": 'NaN' -> " + Number('NaN') + "\n"; res = res + isNaN(NaN) + ": NaN -> " + Number(NaN) + "\n"; res = res + isNaN(0 / 0) + ": 0 / 0 -> " + Number(0 / 0) + "\n"; console.log(res) That shows the results that are expected. I agree that isNaN should be associated with parseFloat and parseInt though, because that's usually when you need to test. I typically convert first and then test to see if that result is NaN instead of using isNaN on the value to be converted. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now