Table of Contents
What concat() Does
- Joins arrays together: it merges two or more arrays into one.
- Returns a new array: the result is separate from the arrays you started with.
- Leaves the originals alone: nothing is changed, added, or removed in them.
- Accepts single values too: you can add individual items, not only arrays.
let first = [1, 2, 3]; let second = [4, 5, 6]; let combined = first.concat(second); console.log(combined); // [1, 2, 3, 4, 5, 6] console.log(first); // [1, 2, 3], unchanged
Syntax and Parameters
array.concat(value1, value2, ...);
No arguments: you get a copy of the original array.
let a = [1, 2]; console.log(a.concat([3], [4, 5])); // [1, 2, 3, 4, 5] console.log(a.concat(6, 7)); // [1, 2, 6, 7] console.log(a.concat()); // [1, 2], a copy
Practical Examples
Merging two lists of names:let team1 = ["Vi", "Sam"]; let team2 = ["Alex", "Priya"]; console.log(team1.concat(team2)); // ["Vi", "Sam", "Alex", "Priya"]Adding items without changing the original:
let cart = ["laptop", "mouse"];
let updated = cart.concat("keyboard");
console.log(cart); // ["laptop", "mouse"]
console.log(updated); // ["laptop", "mouse", "keyboard"]
Combining several arrays at once:
let a = [1]; let b = [2]; let c = [3]; console.log(a.concat(b, c)); // [1, 2, 3]Chaining calls:
let result = [1].concat([2]).concat([3]); console.log(result); // [1, 2, 3]
concat() on Strings
Strings have their own concat(): it joins pieces of text together.
let greeting = "Hello";
let message = greeting.concat(", ", "world", "!");
console.log(message); // "Hello, world!"
In practice, most people use + or template literals instead: they are shorter and easier to read.
let name = "Vi";
console.log("Hello, " + name); // "Hello, Vi"
console.log(`Hello, ${name}`); // "Hello, Vi"
Things to Remember
It only flattens one level: if an array is passed in, its items are added individually, but any arrays nested inside it stay nested.let nested = [1, 2].concat([3, [4, 5]]); console.log(nested); // [1, 2, 3, [4, 5]]The copy is shallow: objects inside the arrays are shared with the originals, not duplicated.
let users = [{ name: "Vi" }];
let more = users.concat([{ name: "Sam" }]);
more[0].name = "Changed";
console.log(users[0].name); // "Changed", because it is the same object
It does not change the original, so you must save the result in a variable.
let nums = [1, 2]; nums.concat([3]); console.log(nums); // [1, 2], the result was thrown away
Common Mistakes
- Forgetting to save the result: concat() does not change the original, so the combined array is lost if you do not store it.
- Expecting deep flattening: nested arrays stay nested. Use flat() if you need them flattened.
- Expecting a deep copy: objects inside are still shared between the original and the new array.
- Using it to add items to an existing array: push() is the right tool for that.
Conclusion
The concat() method is a simple, safe way to join arrays without changing the originals. Remember to save the result, that it flattens only one level, and that the spread operator does the same job in a shorter way.
Frequently Asked Questions (FAQs)
1. Does concat() change the original array?
2. What is the difference between concat() and push()?No. It returns a new array and leaves the originals alone.
3. Is concat() the same as the spread operator?concat() returns a new combined array, while push() adds items to the existing array and changes it.
For combining arrays, yes. [...a, ...b] gives the same result as a.concat(b), and it is more common in modern code.
0 Comments