Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
How to Merge Two Arrays Without Creating a New Array in JavaScript?
Next article icon

How to Merge Two Arrays Without Creating a New Array in JavaScript?

Last Updated : 07 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Given two arrays, the task is to merge both arrays to make a single array without creating a new array in JavaScript. It modifies one array in place to store the merged array elements.

We can use the array push() method to insert array elements into another array using Spread Operator. This approach will modify the first array in place and won't create a new array.

Syntax

array1.push( ...array2);
JavaScript
let array1 = [ 1, 2, 3 ]; let array2 = [ 4, 5, 6 ];  array1.push(...array2);  console.log(array1);  

Output
[ 1, 2, 3, 4, 5, 6 ] 

Please Read JavaScript Array Tutorial for complete understanding of JavaScript Array.

Table of Content

  • Using Array concat() Method
  • Using Array splice() Method
  • Using a for Loop

Using Array concat() Method

We can use array concat() method to Merge Two Arrays Without Creating a New Array. It is a very basic method to merge arrays. It takes second array as a parameter and store the merged array to the first array.

Syntax

array1 = array1.concat( array2 );
JavaScript
let array1 = [ 1, 2, 3 ];  let array2 = [ 4, 5, 6 ];  // Store the merged array to first array array1 = array1.concat(array2);  console.log(array1); 

Output
[ 1, 2, 3, 4, 5, 6 ] 

Using Array splice() Method

The array splice() method is used to modify the contents of an array by removing the existing elements and/or by adding new elements.

Syntax

array1.splice( index, removeCount, ...array2 )
JavaScript
let array1 = [ 1, 2, 3 ]; let array2 = [ 4, 5, 6 ];  array1.splice(array1.length, 0, ...array2);  console.log(array1);  

Output
[ 1, 2, 3, 4, 5, 6 ] 

Using a for Loop

We can also use for loop to merge two arrays without creating a new array. We will use for loop with push() method to merge arrays without creating new array.

JavaScript
let array1 = [ 1, 2, 3 ]; let array2 = [ 4, 5, 6 ];  for (let i = 0; i < array2.length; i++) {     array1.push(array2[i]); }  console.log(array1); 

Output
[ 1, 2, 3, 4, 5, 6 ] 

Next Article
How to Merge Two Arrays Without Creating a New Array in JavaScript?

G

geethabtrweh
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • javascript-array
  • JavaScript-Program

Similar Reads

    JavaScript Program to Merge two Sorted Arrays into a Single Sorted Array
    In this article, we will cover how to merge two sorted arrays into a single sorted array in JavaScript. Given there are two sorted arrays, we need to merge them into a single sorted array in JavaScript. We will see the code for each approach along with the output. Below are the possible approaches t
    3 min read
    JavaScript Program to Transform Nested Array into Normal Array
    JavaScript allows us to convert nested arrays into a single, flat array using JavaScript. By simplifying nested structures, you can enhance the manageability and readability of your code. Example: Input: [[1, 2], [3, [4, 5]]] Output: [1, 2, 3, 4, 5]Below are the approaches to transform nested arrays
    3 min read
    JavaScript Program to Create an Array of Unique Values From Multiple Arrays Using Set Object
    We have given multiple arrays consisting of numerical values, and our task is to create an output array that consists of unique values from the multiple input arrays. We will use the Set object in JavaScript language. Example: Input: inputarray1: [1,2,3,4,5] inputarray2: [4,5,6,7,8] Output: outputAr
    5 min read
    JavaScript Program to Find if Arrays are Disjoint or Not
    Given two arrays, Our task is to check if the given two arrays are disjoint or not. Disjoint arrays in JavaScript are arrays that have no elements in common Input: arr1[] = [12, 34, 11, 9, 3] arr2[] = [2, 1, 3, 5]Output: Not Disjoint3 is common in two sets.Input: arr1[] = [12, 34, 11, 9, 3] arr2[] =
    3 min read
    How to Add a key/value Pair to Map in JavaScript ?
    This article will demonstrate how we can add a key-value pair in the JavaScript map. JavaScript Map is a collection of key-value pairs in the same sequence they are inserted. These key values can be of primitive type or the JavaScript object. All methods to add key-value pairs to the JavaScript Map:
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences