Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

220
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda