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

136
Visualizações
Javascript splice gives different output

Here is a strange behavior I encountered while using splice.

const numbers = [1, 2, 3];
numbers.splice(0, 0, 4, 5);
console.log(numbers); // This gives output [4, 5, 1, 2, 3] 

console.log([1, 2, 3].splice(0, 0, 4, 5)) // Outputs []

Why is that?

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

0

This is the expected behavior. splice returns the array of deleted items, in your case, there are no elements deleted. splice actually modifies the original array. Read the docs here

about 4 years ago · Juan Pablo Isaza Relatório

0

Array.splice function by itself returns the range of elements you have starting from the first argument and ending at the second argument. Everything that comes after the second argument automatically is being added at the end of your original array, BUT it is not returned as a result of the operation.

const numbers = [1, 2, 3];
console.log(numbers.splice(0, 0, 4, 5)) // <- prints nothing because your sequence is from 0 to 0.
about 4 years ago · Juan Pablo Isaza Relatório

0

splice method doesn't return the output, rather modifies the original array.

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