concat()
ES3+Merges two or more arrays into a new array.
Syntax
array.concat(value1, value2, ...)Parameters
values Array | any Arrays or values to concatenate
Return Value
A new array instance
Examples
const a = [1, 2];
const b = [3, 4];
const c = a.concat(b);
console.log(c); 📌 When to Use
Use concat() when you need to merge multiple arrays or add values to an array without modifying the original. It's ideal for combining data sources or creating new arrays from existing ones in an immutable way.
⚠️ Common Mistakes
Using concat() when push() would be more appropriate for mutating operations
Forgetting that concat() creates a shallow copy - nested arrays/objects are still referenced
Using concat() in a loop to build an array - very inefficient, use push() or spread instead
✅ Best Practices
Modern alternative: use spread operator [...arr1, ...arr2] for cleaner syntax
concat() flattens one level of arrays passed as arguments automatically
Use concat() for explicit immutability in codebases that don't use spread
⚡ Performance Notes
concat() creates a new array each time, which is O(n+m) where n and m are the array sizes. The spread operator [...arr1, ...arr2] has similar performance. For building arrays in loops, use push() into a pre-allocated array instead.
🌍 Real World Example
Combining Search Results from Multiple APIs
Merge results from different data sources into a single array
const googleResults = await searchGoogle(query);
const bingResults = await searchBing(query);
const localResults = searchLocalDB(query);
// Combine all results (concat doesn't modify originals)
const allResults = googleResults
.concat(bingResults)
.concat(localResults);
// Modern equivalent with spread:
const allResults2 = [...googleResults, ...bingResults, ...localResults];