Object.entries in JavaScript

Level 3150 keystrokes

Uses Object.entries() with for...of loop to iterate over object key-value pairs.

JavaScript code

// Create an object with subject-score pairs
1const scores = { math: 90, english: 85, science: 95 };
// Object.entries() returns [key, value] pairs; for...of loops over them
// [subject, score] destructures each pair into key and value
2for (const [subject, score] of Object.entries(scores)) {
// Prints each pair, e.g. "math: 90"
3 console.log(`${subject}: ${score}`);
4}

Output

math: 90
english: 85
science: 95
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.

  • :× 4
  • {× 4
  • }× 4
  • ,× 3
  • (× 3
  • )× 3
  • ;× 2
  • .× 2
  • `× 2
  • $× 2

More JavaScript examples at level 3

← JavaScript typing practice