21 February 2024

In JavaScript, for iterating over objects, I recommend using the Object.entries() method, introduced in ECMAScript 2017 (ES8) and supported by modern browsers. This method provides key-value pairs in an array format, offering readability and ease of use. By destructuring within a for...of loop, developers can efficiently access both keys and values, simplifying the process compared to traditional methods like for...in loops or Object.keys().

Source code viewer
  1. const obj = { a: 1, b: 2, c: 3 };
  2.  
  3. for (const [key, value] of Object.entries(obj)) {
  4. console.log(`${key} : ${value}`);
  5. }
Programming Language: ECMAScript