Object Spread in JavaScript

Level 297 keystrokes

The spread operator (...) can merge objects by copying their properties.

JavaScript code

// Create first object with property a
1let obj1 = { a: 1 };
// Create second object with property b
2let obj2 = { b: 2 };
// Use spread to merge both objects into a new one
// ...obj1 copies a: 1, ...obj2 copies b: 2
3let merged = { ...obj1, ...obj2 };
// Print the merged object
4console.log(merged);

Output

{ a: 1, b: 2 }
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this JavaScript code and how often they appear.

  • .× 7
  • ;× 4
  • {× 3
  • }× 3
  • =× 3
  • :× 2
  • ,× 1
  • (× 1
  • )× 1

More JavaScript examples at level 2

← JavaScript typing practice