copy arrays javascript code example
Example 1: copy array javascript
const sheeps = ['Apple', 'Banana', 'Juice'];
const cloneSheeps = sheeps.slice();
const cloneSheepsES6 = [...sheeps];
Example 2: javascript duplicate an array
let arr =["a","b","c"];
const duplicate = [...arr];
const duplicate = Array.from(arr);
Example 3: javascript copy array
var fruit = ["apple", "banana", "fig"];
console.log(fruit);
var fruit2 = fruit.slice();
console.log(fruit2);
var fruit3 = fruit.slice(0,2);
console.log(fruit3);
Example 4: js copy part of array
let arr = ["apple", "kiwi", "banana", "pear"];
let newArray = arr.slice(1, 2)
console.log(newArray);