Ask any question about JavaScript here... and get an instant response.
How can I merge two arrays without duplicates in JavaScript?
Asked on Oct 16, 2025
Answer
To merge two arrays without duplicates in JavaScript, you can use the Set object to automatically handle duplicate values, combined with the spread operator to merge the arrays.
<!-- BEGIN COPY / PASTE -->
const array1 = [1, 2, 3];
const array2 = [3, 4, 5];
const mergedArray = [...new Set([...array1, ...array2])];
console.log(mergedArray); // Output: [1, 2, 3, 4, 5]
<!-- END COPY / PASTE -->Additional Comment:
✅ Answered with JavaScript best practices.- The Set object automatically removes duplicate values.
- The spread operator "..." is used to merge "array1" and "array2" into a new array.
- The final result is stored in "mergedArray", which contains only unique values from both arrays.
Recommended Links:
