How to usepush() Method in Javascript
In JavaScript, to copy array items into another array using the push method, iterate over the given array and use push to add each item to our result array
Syntax:
arr.push(element0, element1, … , elementN)
Example: In this example, we use the push() method to copy array items into another array.
// Source array
let array1 = [10, 20, 30, 40, 50];
let result = [];
// Copy items from array1 to result using push()
for (let i = 0; i < array1.length; i++) {
result.push(array1[i]);
}
console.log(result);
Output
[ 10, 20, 30, 40, 50 ]
Copy Array Items into Another Array in JavaScript
We will see how to copy array items into another array in JavaScript. This operation is valuable when you need to generate a new array containing either identical or a subset of elements from an original array.
These are the following ways to solve this problem:
Table of Content
- Using the Spread Operator (…)
- Using slice() Method
- Using apply() Method
- Using Array.concat() Method
- Using push() Method
- Using map() Method
Contact Us