Skip to content
the poor coder

object values javascript

Poor Man
Published
Reading time
1 min read
On this page

Object Values in JavaScript#

JavaScript is an object-oriented programming language, and objects are one of the fundamental data types. Objects in JavaScript are collections of key-value pairs, where each key is a string (or a symbol) and each value can be of any data type, including other objects.

Accessing Object Values#

To access the values of an object in JavaScript, you can either use the dot notation or the bracket notation.

JavaScript

const person = {
  name: "John",
  age: 30,
  address: {
    city: "New York",
    state: "NY"
  }
};

// Dot notation
console.log(person.name); // Output: John
console.log(person.address.city); // Output: New York

// Bracket notation
console.log(person["name"]); // Output: John
console.log(person["address"]["city"]); // Output: New York

Iterating over Object Values#

You can use a for...in loop to iterate over the values of an object in JavaScript.

JavaScript

const person = {
  name: "John",
  age: 30,
  address: {
    city: "New York",
    state: "NY"
  }
};

for (const key in person) {
  console.log(person[key]);
}
// Output: John
//         30
//         {city: "New York", state: "NY"}

Alternatively, you can use the Object.values() method to get an array of the object's values.

JavaScript

const person = {
  name: "John",
  age: 30,
  address: {
    city: "New York",
    state: "NY"
  }
};

const values = Object.values(person);
console.log(values); // Output: ["John", 30, {city: "New York", state: "NY"}]

Conclusion#

Object values in JavaScript are the values associated with the keys in an object. You can access them using dot notation or bracket notation, and iterate over them using a for...in loop or the Object.values() method.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC