How to use Object.values() in JavaScript

Today, we're learning about Object.values().

Don't worry; it’ll be light and easy.

Let's go!

What is Object.values()?

Object.values() is a method that allows you to see all the values inside an object. Think of it as a way to list out all the contents without their associated names.

How Does It Work?

Consider you have an object like this:

let carDetails = {
  brand: 'Toyota',
  color: 'red',
  type: 'sedan'
};

In this object, you have names (like "brand") and values (like "Toyota").

When you use Object.values(carDetails), you'll get a list of the values: ['Toyota', 'red', 'sedan']. This method gives you only the values, leaving out the names.

Why Use Object.values()?

  • Efficiency: It's a quick method to extract all the values from an object.
  • Simplicity: It provides a clean list of values without the associated names, making it easier to process or view.

Example

Let's say you want to check if any of the car details include the word "red".

By using Object.values(), you can quickly get a list of values and then check if "red" is one of them.

Anything that makes objects iterable is always very handy in your toolbox.

Happy coding! 🦄

JavaScriptJS
Avatar for Niall Maher

Written by Niall Maher

Founder of Codú - The web developer community! I've worked in nearly every corner of technology businesses: Lead Developer, Software Architect, Product Manager, CTO, and now happily a Founder.

Loading

Fetching comments

Hey! 👋

Got something to say?

or to leave a comment.