Is a comparison of an integer value with a NaN data safe in Javascript? For example, using jQuery:
var notaValidaEliminatoria = grupoEliminatorio.notaEliminatoriaValida();
var notaEliminatoria = parseInt(componenteEliminatorio.grupoEliminatorio.val());
if(notaValidaEliminatoria > notaEliminatoria) {
...
}
The variable notaEliminatoria
can contain NaN
if the value of componenteEliminatorio.grupoEliminatorio.val()
(a field in a form) is empty. Will it always happen that any integer value is less than or equal to NaN
or am I forced to detect with isNaN()
whether or not it is a number and if it is do the comparison?
In this part of the code:
If either variable (or both) is NaN the reresult will always be
false
, which is probably not expected behavior.First you should validate with
isNaN()
the variable that could be NaN, (in this case apparentlynotaEliminatoria
due toparseInt()
) and then you decide what to do about it.For example:
Or instead of
throw
doing areturn
method or whatever is appropriate in your case.In this specific case, if you do the
parseInt()
:I think it is not necessary, since it
notaEliminatoria
would always have an integer value, if an error does not occur before in the conversion.I would choose to validate
componenteEliminatorio.grupoEliminatorio.val()
before performing theparserInt()
.