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

218
Vistas
How does this code to create an array of sequential numbers work?

I found this code online and it works; however, I can't work out how!

Can anyone please explain how this code works?

const arr_seq = Array.apply(null, {
  length: 10
}).map(Number.call, Number);

console.log(arr_seq)

Output is:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Also, can anyone explain why this code, which I thought might do exactly the same thing, instead creates an array filled with undefined?

const arr_undef = Array(n).map(Number.call, Number);
about 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Array.apply expects an array(-like) value for its second argument. It will then create an argument for each slot in this array-like object.

Since this code passes { length: 5 } as argument, the apply method will call Array with 5 values. But when reading { length: 5 }[i] for i in the range 0..4, it will always get undefined. So Array.apply(null, { length: 5 }) will translate to Array(undefined, undefined, undefined, undefined, undefined).

Then the .map call has a second argument, which is the thisArg argument. This ensures that not just call is called, but Number.call, i.e. with the right this setting. With Number.call the first argument will receive those undefined values (explained in the previous paragraph) and as second argument, the index of the mapping. So we get these calls:

Number.call(undefined, 0)
Number.call(undefined, 1)
Number.call(undefined, 2)
Number.call(undefined, 3)
Number.call(undefined, 4)

This gives the same result as:

Number(0)
Number(1)
Number(2)
Number(3)
Number(4)

And .map will return an array with those values.

Newer ways to do this

Since ECMAScript 2015, JavaScript has Array.keys() and spread syntax for array literals. So we can now achieve the same in a less cryptic way:

console.log([...Array(5).keys()]);

about 4 years ago · Santiago Trujillo Denunciar

0

Array(n) creates an empty array of length 10; the map/Number.call part requires something to be there to get the index:

const arr = Array(5).fill(null).map(Number.call, Number);
console.log(arr);

about 4 years ago · Santiago Trujillo 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