Skip to content

Latest commit

 

History

History
38 lines (29 loc) · 564 Bytes

File metadata and controls

38 lines (29 loc) · 564 Bytes

Arrays

Copy array

const arr1 = ['JAN', 'FEB', 'MAR', 'APR', 'MAY'];
let arr2;

(function() {
  arr2 = arr1; // Copy array instance
  arr1[0] = 'potato'
})();

console.log(arr2);

Output:

["potato", "FEB", "MAR", "APR", "MAY"]

Copy array elements (Spread Operator)

const arr1 = ['JAN', 'FEB', 'MAR', 'APR', 'MAY'];
let arr2;

(function() {
  arr2 = [...arr1]; // Spread Operator - Copy array elements
  arr1[0] = 'potato'
})();

console.log(arr2);

Output:

["JAN", "FEB", "MAR", "APR", "MAY"]