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

186
Vistas
How array of 2 has length 2?

As it's common arrays in js if you put 3 elements in array it will has length 3 but if I make array with only number two, it has length 2 ?!

const myArry=new Array(2)
console.log(myArry.length) // 2
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

new Array(2) means "make me an array with two empty slots". So yes, it has length 2.

You may be looking for const myArry = [2] which has length 1.

EDIT

Just for fun, if you really wanted to use constructor syntax instead of array literal syntax (the [2] part), you could do:

new Array(1).fill(2)

Which actually could make sense if both the length and the fill value were dynamic parameters.

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