Menu
Posted At Wed, Oct 18, 2023 1:50 PM

JavaScript Object

Listen | 2 min read

Table of Contents

1. Introduction to JavaScript Object
2. Creating Objects
3. Accessing Object Properties
4. Modifying Object Properties
5. Adding and Removing Properties
6. Object Methods
7. Object Constructor
8. Conclusion

1. Introduction to JavaScript Object

In JavaScript, an object is a versatile data structure that allows you to store various data types and entities. Objects are the building blocks of JavaScript, and they play a crucial role in web development.

2. Creating Objects

You can create an object using object literals:

const person = {
  firstName: 'John',
  lastName: 'Doe',
  age: 30
};

3. Accessing Object Properties

To access object properties, you can use dot notation or bracket notation:

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

4. Modifying Object Properties

Object properties can be changed by simply assigning a new value:

person.age = 31;

5. Adding and Removing Properties

You can add new properties to an object:

person.city = 'New York';

To remove a property, you can use the delete operator:

delete person.age;

6. Object Methods

An object can contain functions as well, which are called object methods:

const car = {
  brand: 'Ford',
  model: 'Mustang',
  start: function() {
    console.log('Engine started.');
  }
};

car.start(); // Output: Engine started.

7. Object Constructor

You can create multiple objects with the same structure using a constructor function:

function Person(firstName, lastName) {
  this.firstName = firstName;
  this.lastName = lastName;
}

const johnDoe = new Person('John', 'Doe');

8. Conclusion

JavaScript objects are essential in web development, allowing you to represent and manipulate complex data. By mastering object creation, property access, modification, and methods, you can unlock the full potential of JavaScript in your projects.

Next one is learning about JavaScript Prototypal Inheritance.

Click this link! 👇
JavaScript Prototypal Inheritance

1440 1

Comments