Objects in JavaScript
In this tutorial, we will learn about objects in JavaScript.
Object-oriented programming (OOP) is a programming paradigm that uses objects and their interactions to design applications and computer programs. In JavaScript, objects are a fundamental concept, and they are used to represent real-world entities, such as a person, a car, or a bank account.
What is an Object?
An object is a collection of key-value pairs, where each key is a property and each value is a data or a function. The properties of an object can be accessed using dot notation or bracket notation.
Here is an example of an object that represents a person:
const person = {
firstName: "John",
lastName: "Doe",
age: 30,
fullName: function () {
return this.firstName + " " + this.lastName;
},
};
console.log(person.firstName); // Output: John
console.log(person["lastName"]); // Output: Doe
console.log(person.fullName()); // Output: John DoeIn the example above, the person object has four properties: firstName, lastName, age, and fullName. The fullName property is a function that returns the full name of the person.
Creating Objects
There are several ways to create objects in JavaScript:
1. Object Literal
The simplest way to create an object is by using an object literal notation, which is a comma-separated list of key-value pairs enclosed in curly braces {}.
const person = {
firstName: "John",
lastName: "Doe",
age: 30,
};2. Object Constructor
You can also create an object using the Object constructor function, which is a built-in function in JavaScript.
const person = new Object();
person.firstName = "John";
person.lastName = "Doe";
person.age = 30;3. Object.create() Method
Another way to create an object is by using the Object.create() method, which creates a new object with the specified prototype object.
const person = Object.create({
firstName: "John",
lastName: "Doe",
age: 30,
});Accessing Object Properties
You can access the properties of an object using dot notation or bracket notation.
1. Dot Notation
You can access the properties of an object using the dot notation, which is a period . followed by the property name.
console.log(person.firstName); // Output: John
console.log(person.lastName); // Output: Doe2. Bracket Notation
You can also access the properties of an object using the bracket notation, which is a pair of square brackets [] with the property name inside.
console.log(person["firstName"]); // Output: John
console.log(person["lastName"]); // Output: DoeAdding and Modifying Object Properties
You can add new properties to an object or modify existing properties using the dot notation or bracket notation.
1. Adding Properties
You can add new properties to an object by assigning a value to a new property.
person.age;
person.age = 35;
console.log(person.age); // Output: 352. Modifying Properties
You can modify existing properties of an object by assigning a new value to the property.
person.age = 40;
console.log(person.age); // Output: 40Deleting Object Properties
You can delete properties of an object using the delete operator.
delete person.age;
console.log(person.age); // Output: undefinedObject Methods
An object can also have methods, which are functions that are associated with the object. You can define methods as properties of an object.
const person = {
firstName: "John",
lastName: "Doe",
fullName: function () {
return this.firstName + " " + this.lastName;
},
};
console.log(person.fullName()); // Output: John DoeIn the example above, the fullName property is a method that returns the full name of the person.
Conclusion
- An
objectis a collection ofkey-value pairs, where each key is apropertyand each value is adataor afunction. - You can create objects using
object literal,Object constructor, orObject.create()method. - You can access object properties using
dot notationorbracket notation. - You can add, modify, or delete object properties.
- An object can have
methods, which are functions that are associated with the object.
How is this guide?
Sign in to share your feedback
Help us improve by sharing your thoughts on this guide.
Last updated on