Categories
JavaScript Answers

How to copy array by value with JavaScript?

Spread the love

To copy array by value with JavaScript, we use the slice method.

For instance, we write

const oldArray = [1, 2, 3, 4, 5];
const newArray = oldArray.slice();

to call oldArray.slice with no arguments to make a copy of oldArray and return it.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published.