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

108
Visualizações
JavaScript destructuring with an array element

I have met the following questions in JavaScript:

const [x1, ...[result]] = [3, 4, 5]
console.log([result])

I know x1 is 3, but why is the logging result [4] instead of [4,5]?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

So basically what is happening if we follow this syntax

const [a,...b] = [3,4,5]

Javascript creates an array called b and has the value [4,5]

But in your case what is happening is,

const [a,...[b]] = [3,4,5]

This is essentially assigning to only the first variable of the empty array with first value as b, which always equals 4 and not [4,5] as you expect.

So it's equivalent to the case below

const [a,...[b,c]] = [3,4,5]

the only difference is that you are not providing a variable c in your case. So b would correspond to 4 and c would correspond to 5

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