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.

9
  • Is there a compact way to have this version also merge an array when it finds one in the arguments? Commented Mar 30, 2013 at 23:56
  • I don't understand the first result ["b", "X", "Y", "Z", "c"]. Why isn't "d" included? It seems to me that if you put 6 as the second parameter of slice() and there are 6 elements in the array starting from the specified index, then you should get all 6 elements in the return value. (The doc says howMany for that parameter.) developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Dec 12, 2014 at 2:03
  • Actually, if I use an index of 3 or more, I get nothing in the output (case 1., FireFox) ["a", "b", "c", "d"].insert(2, "X", "Y", "Z").slice(3, 3); => [ ] Commented Dec 12, 2014 at 2:08
  • @AlexisWilke In the first example I used slice method and not splice, which you're referring to in the comment. Second parameter of slice (named end) is zero-based index at which to end extraction. slice extracts up to but not including end. Hence after insert you have ["a", "b", "X", "Y", "Z", "c", "d"], from which slice extracts elements with indices from 1 up to 6, i.e. from "b" to "d" but not including "d". Does it make sense? Commented Dec 12, 2014 at 8:46
  • For some reason, when I go to use insert2, i get an "Expected 1 argument, but got 2" exception. Commented Jun 26, 2020 at 3:08

lang-js