
How can I merge two arrays without duplicates using ES6 features?
Asked on Oct 02, 2025
Answer
To merge two arrays without duplicates in JavaScript using ES6 features, you can utilize the Set object and the spread operator.
<!-- 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 spread operator (...) is used to combine elements from both arrays into a new array.
- The Set object automatically removes any duplicate values.
- The final result is converted back to an array using the spread operator within an array literal.
Recommended Links:
← Back to All Questions