JavaScript - Object

What is Object ?

In JavaScript, an object is a collection of properties, and a property is an association between a name (or key) and a value. A property's value can be a function, in which case the property is known as a method.

const person = {
  name: 'John',
  age: 30,
  greet: function() {
    console.log(`Hello, my name is ${this.name}`);
  }
};

Access the properties of a JavaScript object in several ways:

Dot notation -

You can use the dot notation to access the properties of an object like this:

const person = {
  name: 'John',
  age: 30
};

console.log(person.name);  // Output: "John"
console.log(person.age);  // Output: 30

Bracket notation -

You can also use the bracket notation to access the properties of an object. This is especially useful when you need to access properties using a variable or when the property name contains characters that are not allowed in the dot notation.

To access a property using the bracket notation, you need to pass the property name as a string in the brackets. For example:

const person = {
  name: 'John',
  age: 30
};

console.log(person['name']);  // Output: "John"
console.log(person['age']);  // Output: 30

Iterate JavaScript object -

Using a for-in loop You can use a for-in loop to iterate over the enumerable properties of an object. The for-in loop will iterate over all the properties of an object, including those inherited from the prototype chain.

const person = {
  name: 'John',
  age: 30,
  greet: function() {
    console.log(`Hello, my name is ${this.name}`);
  }
};

for (const property in person) {
  console.log(`${property}: ${person[property]}`);
}

Object method -

Object.keys() -

The Object.keys() method is a built-in JavaScript method that returns an array of a given object's own enumerable property names, in the same order as a normal loop.

Here is an example of how to use Object.keys()

const person = {
  name: 'John',
  age: 30,
  greet: function() {
    console.log(`Hello, my name is ${this.name}`);
  }
};

console.log(Object.keys(person));  // Output: ["name", "age", "greet"]

The Object.keys() method only returns the keys of the own properties of the object, not the keys of the prototype properties.

You can use the Object.keys() method to get an array of the object's keys, and then use a for loop or a for-of loop to iterate over the array and access the object's properties.

Here is an example of how to use Object.keys() with a for loop:

const person = {
  name: 'John',
  age: 30,
  greet: function() {
    console.log(`Hello, my name is ${this.name}`);
  }
};

const keys = Object.keys(person);

for (let i = 0; i < keys.length; i++) {
  const key = keys[i];
  console.log(`${key}: ${person[key]}`);
}

Object.values() -

The Object.values() method is a built-in JavaScript method that returns an array of a given object's own enumerable property values, in the same order as that provided by a for...in loop.

Here is an example of how to use Object.values():

const person = {
  name: 'John',
  age: 30,
  greet: function() {
    console.log(`Hello, my name is ${this.name}`);
  }
};

console.log(Object.values(person));  // Output: ["John", 30, function() { console.log(`Hello, my name is ${this.name}`); }]

The Object.values() method only returns the values of the own properties of the object, not the values of the prototype properties.

You can use the Object.values() method to get an array of the object's values, and then use a for loop or a for-of loop to iterate over the array and access the object's properties.

Here is an example of how to use Object.values() with a for-of loop:

const person = {
  name: 'John',
  age: 30,
  greet: function() {
    console.log(`Hello, my name is ${this.name}`);
  }
};

for (const value of Object.values(person)) {
  console.log(value);
}