4
I’m trying to make a simple sum in a routine:
var sum1 = $('input[name=hdValor01]').val(); // 40
var sum2 = $('input[name=hdValor02]').val(); //
var sum3 = $('input[name=hdValor03]').val(); // 30
console.log(
parseInt(sum1) + parseInt(sum2) + parseInt(sum3)
);
In the console, it is returning Nan. But they are numbers... without the parseInts, it is concatenating the variables.
Any idea? I’ve tried using valueOf() without success.
Are you sure they all have a value? considering
parseInt('')
givesNaN
it would be safer if you had...).val() || 0;
in all thesum
.– Sergio
As is the statement of your inputs?
– Isa
check before if the variables have values, I believe the parseint in the empty variable is causing Nan
– Leo Nogueira
What is the value you are using in
sum2
? It can’t be white.parseInt('')
givesNaN
. And any mathematical operation withNaN
givesNaN
.– bfavaretto
So that’s right... it’s blank in the tests. Thanks for the light! I thought it converted to zero when the variable was null or empty.
– Maykel Esser
Convert to 0 when null or empty using Math.abs()
– Sam