Skip to main content

What are Objects

An object is a collection of key-value pairs where each key is a unique identifier for a value. Objects are used to store and organize data in JavaScript. They are one of the most important data structures in the language and are used extensively in web development.

Object is a more complex data type than primitive data types like numbers, strings, and booleans. Objects can contain multiple values of different types, including other objects, arrays, functions, and more. It is usually used to store related data and functions together. E.g Data of a person, a car, a book, etc.

In this section, we will learn about the basics of objects in JavaScript, including how to create objects, access object properties, and work with object methods.

Creating Objects

In JavaScript, objects can be created using object literals or the Object constructor. Object literals are the most common way to create objects and are defined using curly braces {}.

Here is an example of creating an object using an object literal:

// Creating an object using an object literal
let person = {
name: "John Doe",
age: 30,
isEmployed: true,
};

In the example above, we created an object person with three properties: name, age, and isEmployed. Each property is a key-value pair, where the key is the property name and the value is the property value.

You can also create an object using the Object constructor. The Object constructor can be used to create an empty object or an object with properties.

Here is an example of creating an object using the Object constructor:

// Creating an object using the Object constructor
let person = new Object();
person.name = "John Doe";
person.age = 30;
person.isEmployed = true;

In the example above, we created an object person using the Object constructor and added properties to it using dot notation.

Accessing Object Properties

You can access object properties using dot notation or bracket notation. Dot notation is the most common way to access object properties and is used when the property name is a valid identifier.

Here is an example of accessing object properties using dot notation:

let person = {
name: "John Doe",
age: 30,
isEmployed: true,
};
console.log(person.name); // Output: John Doe
console.log(person.age); // Output: 30
console.log(person.isEmployed); // Output: true

In the example above, we accessed the name, age, and isEmployed properties of the person object using dot notation.

If the property name is not a valid identifier or is stored in a variable, you can use bracket notation to access object properties.

Here is an example of accessing object properties using bracket notation:

let person = {
name: "John Doe",
age: 30,
isEmployed: true,
};

let propertyName = "name";
console.log(person[propertyName]); // Output: John Doe

In the example above, we accessed the name property of the person object using bracket notation and a variable propertyName.

Modifying Object Properties

You can modify object properties by assigning new values to them using dot notation or bracket notation.

Here is an example of modifying object properties using dot notation:

let person = {
name: "John Doe",
age: 30,
isEmployed: true,
};
person.age = 35;
person.isEmployed = false;
console.log(person.age); // Output: 35
console.log(person.isEmployed); // Output: false

In the example above, we modified the age and isEmployed properties of the person object using dot notation.

You can also modify object properties using bracket notation:

let person = {
name: "John Doe",
age: 30,
isEmployed: true,
};
let propertyName = "age";
person[propertyName] = 35;
console.log(person.age); // Output: 35

In the example above, we modified the age property of the person object using bracket notation and a variable propertyName.

Deleting Object Properties

You can delete object properties using the delete operator. The delete operator removes a property from an object.

Here is an example of deleting object properties:

let person = {
name: "John Doe",
age: 30,
isEmployed: true,
};
delete person.age;
console.log(person.age); // Output: undefined

In the example above, we deleted the age property of the person object using the delete operator.

Object Methods

Objects can contain functions as properties, known as object methods. Object methods are used to define behavior for objects and are called using dot notation.

Here is an example of defining an object method:

let person = {
name: "John Doe",
age: 30,
isEmployed: true,
greet: function () {
console.log(`Hello, my name is ${this.name}`);
},
};
person.greet(); // Output: Hello, my name is John Doe

In the example above, we defined an object method greet that logs a greeting message using the name property of the person object. Object methods are just like functions peculiar to the object and they are defines within the object as a property.

Note: The this keyword refers to the object that the method is called on. In this case, this refers to the person object.

In the next sections, we will learn about object destructuring, object cloning, and object comparison in JavaScript.

Made with ❤️ by Fasakin Henry