0Pricing
JavaScript Academy · Lesson

Safe object merging; freezing & sealing

Merge objects without surprises, avoid prototype pollution, and protect data with Object.freeze and Object.seal.

Why safe merging?

Goal: Merge safely and protect state.

  • Shallow merge with spread or Object.assign
  • Avoid prototype pollution by filtering keys
  • Use Object.create(null) for safe targets
  • freeze and seal to lock objects
Safe object merging; freezing & sealing — illustration 1

Shallow merge (spread)

Use spread (or Object.assign({}, a, b)) to make a new object; do not mutate inputs.

// Shallow merge with spread keeps sources unchanged
const a = { x: 1, y: 2 };
const b = { y: 9, z: 3 };

// Later keys win on conflicts
const merged1 = { ...a, ...b };

console.log("a:", a);
console.log("b:", b);
console.log("merged1:", merged1);
Safe object merging; freezing & sealing — illustration 2

All lessons in this course

  1. Input validation, escaping basics
  2. Safe object merging; freezing & sealing
  3. Error boundaries (conceptual) without frameworks
← Back to JavaScript Academy