Objects in JavaScript

Objects in JavaScript

·

3 min read

JavaScript objects are a fundamental part of the language and are used to store and manipulate data. Objects are a collection of key-value pairs where the keys are strings (or symbols in ES6+) and the values can be of any data type, including other objects. Objects provide a way to organize and structure data, making it easier to access and modify.

Creating Objects: There are several ways to create objects in JavaScript:

1. Object Literal:

The simplest way to create an object is by using the object literal syntax, where you define the object properties and their values within curly braces {}.

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

2. Object Constructor:

Another way to create an object is by using the Object constructor and the new keyword.

const person = new Object();
person.name = "John";
person.age = 30;
person.city = "New York";

3. Object.create:

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

const person = Object.create(null);
person.name = "John";
person.age = 30;
person.city = "New York";

Accessing Object Properties: You can access object properties using dot notation (object.property) or bracket notation (object['property']).

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

Modifying Object Properties: You can modify object properties by assigning new values to them.

person.age = 31;
person['city'] = "San Francisco";

Adding and Removing Properties: You can add new properties to an object by assigning values to them. Similarly, you can delete properties using the delete keyword.

person.gender = "Male";  // Adding a new property
delete person.city;     // Removing the 'city' property

Iterating over Object Properties: To iterate over the properties of an object, you can use a for...in loop.

for (let key in person) {
  console.log(key + ": " + person[key]);
}

Object Methods: Objects can also have methods, which are simply object properties that are functions.

const person = {
  name: "John",
  age: 30,
  city: "New York",
  greet: function() {
    console.log("Hello, my name is " + this.name);
  }
};

person.greet();  // Output: Hello, my name is John

Inheritance: JavaScript objects can inherit properties and methods from other objects using prototypes. Every object has an internal link to its prototype, which is another object.

const parent = {
  parentMethod: function() {
    console.log("This is a parent method.");
  }
};

const child = Object.create(parent);
child.childMethod = function() {
  console.log("This is a child method.");
};

child.parentMethod();  // Output: This is a parent method.
child.childMethod();   // Output: This is a child method.

This is just a brief overview of JavaScript objects. Objects in JavaScript are a vast topic, and there are many more concepts to explore, such as object constructors, prototypes, classes (introduced in ES6), and more.