Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

226
Views
I am trying to get a result of addition of number array in Javascript but its showing Nan result

// I want to add a number array elements but the result is showing Nan why so

let numarr = [5598, 4589 ,25465164]
    let len = numarr.length;
    let i,  numval = 0;
    for(i=0; i <= len; i++) {
      numval +=  numarr[i];
    } 
    document.getElementById("demo").innerHTML = numval;
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

It is because of the <= in for-loop. If you use <= then loop will run past the length of the array and it will try to access the element that is not present.

If you are trying to access the element that is not present then it will return undefined. Addition of a number with undefined will give you NaN

Either use numarr.length - 1 or i < len

let numarr = [5598, 4589, 25465164]
let len = numarr.length;
let i, numval = 0;
for (i = 0; i < len; i++) {
  numval += numarr[i];
}
document.getElementById("demo").innerHTML = numval;
<h1 id="demo"></h1>

Alternate solution: You can also use reduce here to achieve the same result

let numarr = [5598, 4589, 25465164]

const numval = numarr.reduce((a, c) => a + c);

document.getElementById("demo").innerHTML = numval;
<h1 id="demo"></h1>

about 4 years ago · Juan Pablo Isaza Report

0

Your array has 3 elements at indices 0, 1 and 2. So your arrays should run from 0 to array.length - 1. If you tries to access numarr[3] the value will be undefined. Adding undefined to a number is NaN that means not a number

It should be i < len

let numarr = [5598, 4589, 25465164];
let len = numarr.length;
let i, numval = 0;
for (i = 0; i < len; i++) {
  numval += numarr[i];
}
document.getElementById("demo").innerHTML = numval;
<div id="demo"></div>

about 4 years ago · Juan Pablo Isaza Report

0

The mistake you made has already been pointed out in numerous other answers, I would like to show you an alternative in modern JS.

Instead of using an index-based for-loop, Javascript has a specialized function for what you want to do, which is called Array.prototype.reduce. It is called once for each array element, adds its value to the accumulator and returns the accumulator after the last iteration. The start value 0 for the accumulator is the second parameter.

const numarr = [5598, 4589 ,25465164]
const sum = numarr.reduce((accumulator, value) => accumulator+value, 0);
document.getElementById("demo").innerHTML = sum;
<div id="demo"></div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!