Object in JavaScript

Objects in JavaScript

  • Objects in JavaScript are essential data structures that allow us to store collections of key-value pairs.

  • They serve as blueprints for organizing and managing data efficiently within our code.

Creating Objects

Objects are created using curly braces {} and consist of key-value pairs. Each key is a unique identifier associated with a value. For example:

let person = {
  name: "Tom",
  age: 27,
  eyeColor: "blue",
  updateAge: function () {
    return ++this.age;
  },
};

Accessing Object Properties

Properties of an object can be accessed

  • using dot notation (object.property)

  • bracket notation (object['property']).

Example:

Manipulating Objects

Adding Properties:

Properties can be added to an object by simply assigning a value to a new key.

Removing Properties

Properties can be removed using the delete keyword.

Object Methods

Objects can contain methods, which are functions stored as property values. These methods can perform operations on the object's data.


Object Constructors

  • Object constructors allow us to create multiple instances of an object with similar properties and methods.

  • We define a blueprint for the object, and then create instances based on that blueprint.

Example:

Advantages of Object Constructors

  • Using object constructors helps in organizing data and reducing redundancy.

  • It allows for the creation of multiple instances of an object with ease, enabling efficient management of related data.

Ref

Last updated