JSFiddle - React, Tailwind, and code Playground

by kman007_us

JavaScript

var obj = {name: "Kevin"};
var arr = [obj];
var shallowCopy = arr.slice();

// arr and shallowCopy both point to the same `obj`
console.log("arr[0] === shallowCopy[0]: ", arr[0] === shallowCopy[0]);
console.log("arr:", JSON.stringify(arr));
console.log("shallowCopy:", JSON.stringify(shallowCopy));

// changing the `obj` will change it in both arrays due to the 
// shallow copy
obj.name = "Rick";
console.log("arr:", JSON.stringify(arr));
console.log("shallowCopy:", JSON.stringify(shallowCopy));