Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

138
Views
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 answers
Answer question

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 Report

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 Report

0

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!