How to Make a Shallow Copy of Array and Object in JavaScript

Published Aug 23, 2020

Modern JavaScript has made shallow copying easier than ever. ES6 (ES2015) introduced the spread operator ..., which essentially dumps the contents of an array or object.

Copy an Array

const original = [1, 2, 3];
const copy = [ ...original ];

Copy an Object

const original = { 
  key1: 1,
  key2: 2
};
const copy = { ...original };

More JS Articles