How to use a for loop In Javascript
This method involves iterating through each element in the original array and copying each element into a new array.
Example: This example shows the implementation of the above-explained approach.
const originalArray = [1, 2, 3];
const clonedArray = [];
for (let i = 0; i < originalArray.length; i++) {
clonedArray.push(originalArray[i]);
}
console.log(clonedArray);
Output
[ 1, 2, 3 ]
How to clone an array in JavaScript ?
In JavaScript, cloning an array means creating a new array with the same elements as the original array. Cloning an array in JavaScript is useful when you want to create a new array that has the same elements as an existing array, without modifying the original array.
Contact Us