JavaScript .entries()

grace_k's avatar
Published Dec 11, 2023
Contribute to Docs

The .entries() method returns an array containing arrays of an object‘s key-value pairs in the following format: [ [key, value], [key, value], ... ].

  • A full-stack engineer can get a project done from start to finish, back-end to front-end.
    • Includes 51 Courses
    • With Professional Certification
    • Beginner Friendly.
      150 hours
  • Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
    • Beginner Friendly.
      15 hours

Syntax

Object.entries(someObject)

The above syntax will return the key-value pair entries for someObject.

Example

In the example below, a drink object is declared. Then a for-of loop is used to format the key-value pairs and output them to the console:

const drink = {
name: 'Chai Latte',
size: 'Regular',
price: 2.99,
};
for (const [key, value] of Object.entries(drink)) {
console.log(`${key.toUpperCase()} ${value}`);
}

This above example will return the following output:

NAME Chai Latte
SIZE Regular
PRICE 2.99

All contributors

Contribute to Docs

Learn JavaScript on Codecademy

  • A full-stack engineer can get a project done from start to finish, back-end to front-end.
    • Includes 51 Courses
    • With Professional Certification
    • Beginner Friendly.
      150 hours
  • Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
    • Beginner Friendly.
      15 hours