Skip to main content

Understanding Object.values()

A quick guide to understanding `Object.values()`

Updated Markdown
Part of seriesExploring JavaScript Object Methods
Understanding Object.values()

πŸ‘‰ The Object.values() method returns an array of all the property values of an object.

πŸ‘‰ Syntax

Object.values(object);

πŸ‘‰ Here are some of the major points you have to remember while using Object.values()

  1. It returns the elements in the same order as that provided by a for...in loop.
  2. The only difference is that a for...in loop enumerates properties in the prototype chain as well.
  3. The result of Object.values() is an array of all the values so you can map all the array methods to it.
  4. This method became part of JavaScript in ES2017.

πŸ‘‰ Example:

const user = {
  name: 'Jhon doe',
  age: 34,
  isActive: false
};

// Using `for...in`
for (let key in user) {
  console.log(user[key]);
}

// Using `Object.values()`
Object.values(user).forEach(value => console.log(value));

πŸ‘‰ References:

πŸ‘‰ Follow me:

About the author

  • A Full Stack Software Engineer(JavaScript) who’s passionate about creating content that can be of value to others. Besides juggling work, content creation and other commitments, I can be found traveling to contemplate the meaning of life under the stars 🌠

HHINA5y ago

This article is amazingπŸ”₯πŸ”₯. I never thought of the difference of for...in loop and Object.values (). After reading every article of the Rehan, I always learn something new.🀩🀩

1

Thank you so much HINA! Keep reading :D