📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
Merging two arrays is a common operation in programming, where the goal is to combine the elements of both arrays into a single array. JavaScript provides several ways to merge arrays, including using the spread operator (...
) or the concat()
method. This program demonstrates how to merge two arrays in JavaScript.
Problem Statement
Create a JavaScript program that:
- Accepts two arrays.
- Merges the two arrays into a single array.
- Returns and displays the merged array.
Example:
Input:
[1, 2, 3]
and[4, 5, 6]
Output:
[1, 2, 3, 4, 5, 6]
Input:
['a', 'b']
and['c', 'd', 'e']
Output:
['a', 'b', 'c', 'd', 'e']
Solution Steps
- Read the Input Arrays: Provide two arrays either as user input or directly within the code.
- Merge the Arrays: Use either the spread operator or the
concat()
method to merge the two arrays. - Display the Result: Print the merged array.
JavaScript Program
// JavaScript Program to Merge Two Arrays // Author: https://www.javaguides.net/ function mergeArrays(arr1, arr2) { // Step 1: Merge the arrays using the spread operator let mergedArray = [...arr1, ...arr2]; // Step 2: Return the merged array return mergedArray; } // Example input let array1 = [1, 2, 3]; let array2 = [4, 5, 6]; let mergedArray = mergeArrays(array1, array2); console.log(`The merged array is: [${mergedArray}]`);
Output
The merged array is: [1, 2, 3, 4, 5, 6]
Example with Different Input
let array1 = ['a', 'b']; let array2 = ['c', 'd', 'e']; let mergedArray = mergeArrays(array1, array2); console.log(`The merged array is: [${mergedArray}]`);
Output:
The merged array is: ['a', 'b', 'c', 'd', 'e']
Explanation
Step 1: Merge the Arrays
- The spread operator (
...
) is used to mergearr1
andarr2
. This operator expands the elements of each array into individual elements, which are then combined into a new array.
Step 2: Return the Merged Array
- The merged array is returned by the function and printed using
console.log()
.
Alternative Method: Using concat()
Another way to merge arrays in JavaScript is by using the concat()
method:
function mergeArrays(arr1, arr2) { return arr1.concat(arr2); }
This also returns a new array that contains the elements of both input arrays.
Conclusion
This JavaScript program demonstrates how to merge two arrays using both the spread operator and the concat()
method. These methods are efficient and easy to use, providing a straightforward solution to combining arrays in JavaScript. The program can handle arrays of any type and size, making it a versatile tool for array manipulation.
Comments
Post a Comment
Leave Comment