0

According to tutorial by w3schools (http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_tostring_number), we can use toString() method on a integer var. Kindly look at the following code:

var num = 15;
var n = num.toString();
alert(isNaN(n));

If toString() method is working, why isNaN(n) returning false?

2
  • 1
    You tried with: var num = 15; var n = num + ""? Commented Jun 11, 2015 at 8:39
  • Mozila and isNaN function Commented Jun 11, 2015 at 8:41

3 Answers 3

3

The IsNaN method tries converting the string passed to it back to a number, and since "15" is still actually a number, the method returns false.

See: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/isNaN

Sign up to request clarification or add additional context in comments.

Comments

1

isNaN() coerces the string '15' into a number value before checking it.

isNaN even coerces booleans, and some falsy values into numbers.

isNaN(true) // >> false
isNaN(false) // >> false
isNaN([]) // >> false
isNaN('') // >> false

Try using typeof to figure out if it's a number or not

var num = 15;
var n = num.toString();
alert(typeof n === 'number');

Comments

-1

isNaN() function returns true if the value is NaN, and false if not. Your code is excute alert(isNaN(15));

So it is return false

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.