Sikau

Advanced JavaScript · Day 9 · about 25 min

Copying, comparing and freezing

References, deep copies and immutability.

Two names, one thing

Change an object through one variable and it changes "everywhere". That surprises everyone once. Objects are passed by reference — let's master copying and protecting them.

References and shallow copies

const a = { name: "Sita", tags: ["admin"] };
const b = a;
b.name = "Gita";
console.log(a.name);

const c = { ...a };
c.name = "Mina";
c.tags.push("editor");
console.log(a.name, a.tags);

console.log({ x: 1 } === { x: 1 });

The rest of this lesson

  • Deep copies and freezingexplain
  • Updating nested data without mutationexplain
  • Predict the outputpredict
  • Shallow copy trappredict
  • Your turnwrite code
  • Badge quizquiz
  • What you learnedrecap

Continue this lesson free — run real code in your browser

Continue with Google

See all 27 lessons of Advanced JavaScript