Clone an array with Javascript

There are several ways to clone a table in Javascript, here are three ways to do it.

  • A basic one that works in ES5
  • A second one, a little more recent and which works in ES6+
  • The last one is very practical and uses JSON to format the raw data of your input table

// ES5
const clone = (arr) => => x);

// ES6
const clone = (arr) => [...arr];

// With JSON
const clone = (arr) => JSON.parse(JSON.stringify(arr));
Related snippets