In ActionScript (NaN==parseFloat(input.text)) warns that it will always be false. Why?

StackOverflow https://stackoverflow.com/questions/150548

  •  02-07-2019
  •  | 
  •  

Question

Despite the rather clear documentation which says that parseFloat() can return NaN as a value, when I write a block like:

if ( NaN == parseFloat(input.text) ) {
  errorMessage.text = "Please enter a number."
}

I am warned that the comparison will always be false. And testing shows the warning to be correct.

Where is the corrected documentation, and how can I write this to work with AS3?

Was it helpful?

Solution

Because comparing anything to NaN is always false. Use isNaN() instead.

OTHER TIPS

isNaN(parseFloat(input.text))

BTW, if for some reason you don't have access to isNaN(), the traditional method is to compare the number to itself:

if( number != number )
{
    //Is NaN 
}

Documentation can be found in the Adobe Flex Language Reference Here as well as other globally available functions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top