Explain concat method

Please explain, how exactly works method concat in this sample. Thank you.


The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.

The Array.prototype.concat() method concatenates the content of two array into one. That is to say, it adds all the contents of the array you pass it as an argument onto the array you call it on.

In this example, the function is first taking a slice of the array passed as the first argument from the beginning up to but not including the index provided as the second argument (["a", "b"]). Then it concatenates the a slice of the same array from the index provided + 1, to the end of the array (["d", "e"]). Finally, the function would return a new array (["a", "b", "d", "e"])

1 Like

Thanks. I understood.