Skip to main content
The 2026 Annual Developer Survey is live— take the Survey today!

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

4
  • 4
    "a totally meaningless empty array" - it only returns an empty array when the second parameter is 0. If it's greater than 0, it returns the items removed from the array. Given that you're adding to a prototype, and splice mutates the original array, I don't think "proper functional programming" belongs anywhere in the vicinity of splice. Commented May 22, 2016 at 21:15
  • We are talking about insert here and the second parameter of Array.prototype.splice() has to be zero. And what it returns have no meaning other than "i have not deleted anything" and since we use it for inserting an item we already have that information. If you don't want to mutate the original array then you can do the same with two Array.prototype.slice() and one Array.prototype.concat() operations. It's up to you. Commented May 22, 2016 at 21:23
  • 1
    Your second implementation is the cleanest from this whole page and you have zero votes. Please take mine and keep up the good work. (you should just avoid mutating the prototype but you already know that) Commented Jun 2, 2016 at 14:18
  • 1
    I think it is worth mentioning that the rest parameter is new ECMA 6th (developer.mozilla.org/en/docs/Web/JavaScript/Reference/… ) Commented Dec 9, 2016 at 17:02

lang-js