Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

185
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda