
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to add two arrays into a new array in JavaScript?
Let’s say the following is our first array −
var firstArray=["John","David","Bob","Mike"];
Following is our second array −
var secondArray=["Chris","Adam","James","Carol"];
To add the above two arrays into a new array, use concat().
Example
Following is the code −
var firstArray=["John","David","Bob","Mike"]; var secondArray=["Chris","Adam","James","Carol"]; var thirdArray=firstArray.concat(secondArray); console.log("First Array value="); console.log(firstArray); console.log("Second Array value="); console.log(secondArray); console.log("Third Array value="); console.log(thirdArray);
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo249.js.
Output
This will produce the following output on console −
PS C:\Users\Amit\javascript-code> node demo249.js First Array value= [ 'John', 'David', 'Bob', 'Mike' ] Second Array value= [ 'Chris', 'Adam', 'James', 'Carol' ] Third Array value= [ 'John', 'David', 'Bob', 'Mike', 'Chris', 'Adam', 'James', 'Carol' ]
- Related Questions & Answers
- Merge arrays into a new object array in Java
- How to combine two arrays into an array of objects in JavaScript?
- How to add a new object into a JavaScript array after map and check condition?
- Digits of element wise sum of two arrays into a new array in C++ Program
- Merging two sorted arrays into one sorted array using JavaScript
- Converting two arrays into a JSON object in JavaScript
- Splitting array of numbers into two arrays with same average in JavaScript
- How to add new value to an existing array in JavaScript?
- Turning a 2D array into a sparse array of arrays in JavaScript
- How to compare two arrays in JavaScript and make a new one of true and false? JavaScript
- Can we convert two arrays into one JavaScript object?
- Add two consecutive elements from the original array and display the result in a new array with JavaScript
- How to add new array elements at the beginning of an array in JavaScript?
- How to concatenate two files into a new file using Python?
- How to merge two arrays in JavaScript?
Advertisements