// This example shows three ways to create a new array from the existing fruits array: first by using spread syntax, then by using the from() method, and then by using the slice() method. const fruits = ['Strawberry', 'Mango']; // Create a copy using spread syntax. const fruitsCopy = [...fruits]; // ["Strawberry", "Mango"] // Create a copy using the from() method. const fruitsCopy = Array.from(fruits); // ["Strawberry", "Mango"] // Create a copy using the slice() method. const fruitsCopy = fruits.slice(); // ["Strawberry", "Mango"]
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter