Table of Contents
- Array.concat() on Object References
- Array.concat on primitive data types
- Concatenating multiple arrays
Array.concat() on Object References
Before working on concatenating arrays, let's look into Object references.
Here, variable watermelon stores a reference to the assigned object. Now, let's store it into the fruit array and use Array.concat() over it.
Here, Watermelon is shallowly copied and the reference to the original object is only copied and not the object itself. Now, the watermelon in both the fruits array and the fruitsAndVegetables array are pointing to the same object. So, if any value of the watermelon object is changed, it will be changed in fruitsAndVegetables array as well.
Array.concat on primitive data types
Array.concat() copies values of primitive data types such as string, number, and boolean into the new array.
Concatenating multiple arrays
To concatenate multiple arrays, we call Array.concat() on one array and pass the rest arrays as arguments.
We will love to see examples and scenarios where you have used Array.concat(). Feel free to add them in the comments below :). Have a nice day.