Object returning NaN when sum values

var jan = 0; //this should solve it

for (var i=0;i<data.length;i++){ 
    if(data[i].jan != null){    
        jan += parseFloat(data[i].jan);
        console.log(jan);
    }
}

Try this should solve it :)

Explanation as quoted by DON in comments below:

var jan; this will declare variable as undefined, so when you try to add values with undefined you will get as NaN, so the answer here with var jan = 0 will work – DON


I like this approach. It basically sets the value to 0 on the first iteration when jan doesn't exist.

jan = (jan || 0) + parseFloat(data[i].jan);

Tags:

Javascript