Simple sum returning Nan

Asked

Viewed 1,355 times

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.

  • 3

    Are you sure they all have a value? considering parseInt('') gives NaN it would be safer if you had ...).val() || 0; in all the sum.

  • 1

    As is the statement of your inputs?

  • 1

    check before if the variables have values, I believe the parseint in the empty variable is causing Nan

  • 2

    What is the value you are using in sum2? It can’t be white. parseInt('') gives NaN. And any mathematical operation with NaN gives NaN.

  • 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.

  • 1

    Convert to 0 when null or empty using Math.abs()

Show 1 more comment

3 answers

4

The OR(||) operator will return the first valor == true, as the return of a ParseInt("") == Nan, for Javascript NaN == false will be returned the second value of your expression, which in case is 0, solving your problem.

Source: https://stackoverflow.com/questions/6736476/how-to-turn-nan-from-parseint-into-0-for-an-empty-string

var sum1 = $('input[name=hdValor01]').val(); // 40
var sum2 = $('input[name=hdValor02]').val(); // 
var sum3 = $('input[name=hdValor03]').val(); // 30
console.log(
    (parseInt(sum1) || 0) + 
    (parseInt(sum2) || 0) + 
    (parseInt(sum3) || 0)
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" value="40" name="hdValor01">
<input type="text" value="" name="hdValor02">
<input type="text" value="30" name="hdValor03">

3

If the result is NaN, one of the terms of the addition is NaN, for all mathematical operations involving NaN yield results NaN. Like parseInt('') also gives NaN, I’d say you’re leaving at least one of the fields blank.

If the intention is to allow blank fields, you will need to check each term before adding, using the function isNaN - which is the only way to verify whether a value is NaN, since NaN !== NaN.

  • The second variable is empty in the tests. I thought it converted to zero when the variable was null or empty. I will take a test to convert it to 0 before the sum then. Thank you! ;-)

3

var sum1 = $('input[name=hdValor01]').val();
var sum2 = $('input[name=hdValor02]').val();
var sum3 = $('input[name=hdValor03]').val();
var somaProd = (Number(sum1) + Number(sum2) + Number(sum3));

That way, it worked! I converted all strings to numbers, including the empty one.

  • Great, I didn’t remember that option.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.