Copy array items into another array via JavaScript

How to copy array items into another array in JavaScript?

Last updated:7/20/2021 1:55:29 AM

1 Answers

Ethan Karla
Ethan Karla

 The slice() method returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included) where start and end represent the index of items in that array. The original array will not be modified.

Let's switch to the example for better understanding :

<!DOCTYPE html>

<html>
<head>
</head>
<body>
</body>
<script>
    function sliced() {
        var arr = ['shift', 'splice', 'filter', 'pop'];
        let newArray = arr.slice();
        document.write('New Array element: ' + newArray + '<br>');
    }
    sliced();
</script>
</html>
// Expected  Output : New Array element: shift,splice,filter,pop

Hope this code will be helpful for you.

Happy Coding!

Answer