Object Keys Iteration in JavaScript

Level 3134 keystrokes

Object.keys() returns an array of property names, allowing iteration over objects.

JavaScript code

// Create an object with subject scores
1const scores = { math: 90, english: 85, science: 95 };
// Object.keys() returns ["math", "english", "science"]
// forEach iterates through each key
2Object.keys(scores).forEach(key => {
// Access the value using bracket notation
// Template literal formats the output
3 console.log(`${key}: ${scores[key]}`);
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
  • )× 3
  • ,× 2
  • `× 2
  • =× 2

More JavaScript examples at level 3

← JavaScript typing practice