Back to all posts

What is a JavaScript Object?


A JavaScript object is a data structure that can store data and associated functions.

In JavaScript, an object is an unordered collection of key-value pairs, where the keys are strings (or symbols in ES6+) and the values can be of any data type, including strings, numbers, Booleans, arrays, functions and other objects. It serves as a container for related data and functionalities, making it a crucial part of JS programming.

How to create a JavaScript object

There are three ways to create a JavaScript object:

  • Using the object literal syntax:
const myObject = {
  name: "Murthi",
  age: 30,
  isMarried: false
};

The simplest way to create an object is by using the object literal notation. It involves defining the object properties and their corresponding values within curly braces.

  • Using the constructor function:
function Person(name, age, profession) {
  this.name = name;
  this.age = age;
  this.profession = profession;
}

const person = new Person("Chethan", 25, "Web Developer");

JavaScript also provides a constructor function to create objects. You can define a custom constructor function using the function keyword and the this keyword to assign values to object properties.

  • Object.create() method:
const person = Object.create(null);
person.name = "John Doe";
person.age = 25;
person.profession = "Web Developer";

The Object.create() method allows you to create a new object with a specified prototype object and properties.


Accessing object properties

Object properties can be accessed using the dot notation or the bracket notation.

  • Dot notation:
const myObject = {
  name: "Murthi",
  age: 30,
  isMarried: false
};

const name = myObject.name; // "Murthi"
  • Bracket notation:
const myObject = {
  name: "Murthi",
  age: 30,
  isMarried: false
};

const name = myObject["name"]; // "Murthi"

Adding and removing object properties

Object properties can be added and removed using the dot notation or the bracket notation.

  • Adding a property:
const myObject = {
  name: "Murthi",
  age: 30,
  isMarried: false
};

myObject.height = 180; // Adds the "height" property to the object
  • Removing a property
const myObject = {
  name: "John Doe",
  age: 30,
  isMarried: false
};

delete myObject.height; // Removes the "height" property from the object

Iterating over object properties

To iterate over the properties of an object, you can use the for ...in loop.

const myObject = {
  name: "John Doe",
  age: 30,
  isMarried: false
};

for (const property in myObject) {
  console.log(property); //"Name", "Age", "isMarried"
}

Object Methods:

In addition to properties, objects can also contain methods, which are functions associated with the object. These methods can be used to perform actions or manipulate object data

const calculator = {
  operand1: 10,
  operand2: 5,
  add: function() {
    return this.operand1 + this.operand2;
  },
  subtract: function() {
    return this.operand1 - this.operand2;
  }
};

console.log(calculator.add());  // Output: 15
console.log(calculator.subtract()); // Output: 5

Object Prototypes and Inheritance:

JavaScript follows a prototype-based inheritance model. Each object in JS has a prototype and objects can inherit properties and methods from their prototype. The prototype. The prototype chain allows for inheritance and efficient memory usage.

// Creating a prototype object
const personPrototype = {
  greet: function() {
    return "Hello, " + this.name + "!";
  }
};

// Creating a new object using the prototype
const person = Object.create(personPrototype);
person.name = "John Doe";

console.log(person.greet()); // Output: "Hello, John Doe!"

Build-in Prototypes:

JavaScript provides built-in prototypes for various types, such as Object.prototype , Array.prototype , and String.prototype. These prototypes contain useful methods that can be accessed by objects of those types.

For example Array.prototype contain methods like push() , pop() , and forEach , which can be uused on arrays

const numbers = [1, 2, 3, 4, 5];
console.log(numbers.length); // Output: 5
console.log(numbers.join(", ")); // Output: "1, 2, 3, 4, 5"
numbers.push(6);
console.log(numbers); // Output: [1, 2, 3, 4, 5, 6]

In the example above, numbers is an array that inherits from Array.prototype. As a result, it has access to methods like length, join(), and push() provided by the Array.prototype.

Conclusion:

The JavaScript object data type is a powerful tool that enables developers to organize and manipulate data in a flexible manner. Understanding objects and their properties, methods, and inheritance mechanisms is essential for effective JavaScript programming. By leveraging objects, you can build complex applications and create reusable code that maximizes efficiency and readability.

Remember to experiment and explore further to unlock the full potential of objects in JavaScript. Happy coding!