Skip to main content

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.

Required fields*

14
  • Is it possible to do concat or spread an array in between an another array? Commented Feb 19, 2018 at 12:08
  • @RameshRajendran In between two other arrays, sure. "In between an other array", not sure what you mean. Commented Feb 19, 2018 at 12:10
  • 1
    @RameshRajendran The equivalent to that would be ['one'].concat(parts, ['two', 'three']) (or ['one'].concat(parts).concat(['two', 'three']) if you don't want to pass multiple arguments) Commented Feb 19, 2018 at 12:13
  • 10
    FWIW, there's a measurable performance difference. See jsperf.com/spread-vs-concat-vs-push Commented Jan 31, 2019 at 23:08
  • 2
    @DrazenBjelovuk .concat(x) makes the reader assume that x is an array as well. Sure, concat can handle non-array values as well, but imo that's not its main mode of operation. Especially if x is an arbitrary (unknown) value, you would need to write .concat([x]) to make sure it always works as intended. And as soon as you have to write an array literal anyway, I say that you should just use spread syntax instead of concat. Commented Sep 6, 2019 at 20:52