Understanding the Fundamentals of JavaScript Objects

JavaScript objects are a data structure that allows you to store multiple pieces of related data together in a single place. They are similar to arrays, but instead of using numerical indexes to access the data, you use keys.

Here’s an example of a JavaScript object:

var person = {
firstName: "Alias",
lastName: "Grace",
age: 30,
isStudent: false,
hobbies: ["reading", "knitting"]
};

In this example, the person object has several properties, such as firstNamelastNameageisStudent, and hobbies. Each property is a key-value pair, where the key is the property name and the value is the data associated with that property.

You can access the properties of an object using the dot notation or the square bracket notation. For example, you can get the value of the firstName property by using person.firstName or person["firstName"].

console.log(person.firstName); // "Alias"
console.log(person["lastName"]); // "Grace"

You can also add, update, or delete properties from an object.

// Add a new property
person.address = "New York City";
console.log(person.address); // "New York City"

// Update a property
person.age = 35;
console.log(person.age); // 35

// Delete a property
delete person.isStudent;
console.log(person.isStudent); // undefined

In this example, we’ve seen how to create an object and how to access, update and delete properties from it. JavaScript objects are very versatile and are commonly used to store and manipulate data in web development. They can also be used to define custom objects, which can be useful when building complex applications.

Hello, I’m Anuj. I make and teach software.

My website is free of advertisements, affiliate links, tracking or analytics, sponsored posts, and paywalls.
Follow me on LinkedIn, X (twitter) to get timely updates when I post new articles.
My students are the reason this website exists. ❤️

Feedback Display Feedback Display