Explore Topics

How to Merge Two Arrays in JavaScript?

Last Updated : 18 Apr, 2025 - Asked By Ashok

javascript  merge 2 arrays 

Answers
2024-01-25 12:45:01

We can merge two arrays in JavaScript using the concat() method. Here’s an example:

const array1 = [1, 2, 3];
const array2 = [4, 5, 6];
const mergedArray = array1.concat(array2);

 

This will create a new array mergedArray that contains all the elements of array1 and array2 in the same order as they were inserted into the original arrays .

If you want to remove duplicates from the merged array, you can use the Set object. Here’s an example:

const array1 = [1, 2, 3];
const array2 = [2, 3, 4];
const mergedArray = [...new Set([...array1, ...array2])];

 

This will create a new array mergedArray that contains all the unique elements of array1 and array2 in the same order as they were inserted into the original arrays.

 

Your Answer



Other Resources

Quiz Image
Quiz

Test your knowledge with interactive quizzes.

Interview Questions Image
Interview Questions

Prepare for interviews with curated question sets.

Q&A Image
Q&A

Ask your coding-related doubts and get answers.

Certification Image
Certification

Earn certifications to enhance your resume.

internships Image
Internships

Hands-on projects to improve your skills.

Quiz Image
Quiz

Test your knowledge with interactive quizzes.

Interview Questions Image
Interview Questions

Prepare for interviews with curated question sets.

blog Image
Blogs

Add your technical blogs and read technical topics.

Certification Image
Certification

Earn certifications to enhance your resume.

Q&A Image
Q&A

Hands-on projects to improve your skills.

People Also Asked