Objects: Storing Complex Data
In this third installment of our Introduction to JavaScript Data Structures series, we’ll dive into one of the most powerful data structures in JavaScript: Objects.
Objects allow us to store data in a structured way using key-value pairs, making them perfect for managing complex data and entities in our applications.
Understanding Objects
An object in JavaScript is a collection of key-value pairs, where each key is either a unique string or a Symbol (a special, immutable data type introduced in ES6). Each value can be any type of data — including other objects, arrays, functions, or primitives.
Objects are incredibly versatile and commonly used to represent real-world entities like user profiles, product listings, or configurations. The key-value pair structure allows us to easily group related data, making it easier to manage and manipulate.
Example: User Profile
const userProfile = {
id: 1,
name: "Alice",
age: 28,
preferences: {
theme: "dark",
notifications: true
}
};In this example, the object `userProfile` stores details about a user, including their ID, name, age, and preferences. The nested `preferences` object holds additional information about the user’s theme and notification settings.
Note: Thanks to a great suggestion by Jon Randy, it’s important to remember that while string keys can be accessed and modified using either dot notation or bracket notation, Symbol keys require bracket notation.
Example with a Symbol key:
const symKey = Symbol("uniqueId");
userProfile[symKey] = 12345;Dot notation won’t work with Symbols, so bracket notation is the way to go in those cases.
Manipulating Objects
Now, let’s explore how to manipulate objects by adding, updating, and deleting properties.
1. Adding and Updating Properties
You can add new properties to an object or update existing ones using dot notation or bracket notation. However, if the key is a Symbol, you must use bracket notation. (Thanks Jon Randy for highlight this).
// Using dot notation with a string key
userProfile.age = 29;
// Using bracket notation with a string key
userProfile["email"] = "alice@example.com";
// Using bracket notation with a Symbol key
const symKey = Symbol("id");
userProfile[symKey] = 12345;
// Output:
{
id: 1,
name: "Alice",
age: 29, // Updated
preferences: {
theme: "dark",
notifications: true
},
email: "alice@example.com" // New property
}2. Deleting Properties
To delete a property, we use the `delete` operator.
// Deleting the 'email' property
delete userProfile.email;
console.log(userProfile);Output:
{
id: 1,
name: "Alice",
age: 29,
preferences: {
theme: "dark",
notifications: true
}
}3. Computed Property Names and Dynamic Keys
One powerful feature of JavaScript objects is computed property names. This allows you to create dynamic keys for your objects, based on variables or expressions.
const dynamicKey = "email";
// Dynamically adding a property
userProfile[dynamicKey] = "alice@example.com";
console.log(userProfile);Output:
{
id: 1,
name: "Alice",
age: 29,
preferences: {
theme: "dark",
notifications: true
},
email: "alice@example.com"
}In this example, the `dynamicKey` variable holds the string `”email”`, which is then used to dynamically set a property on the `userProfile` object.
Nested Objects
Objects can also be nested, meaning that the value of a property can be another object. This is especially useful when you need to store more complex data, such as user preferences, settings, or product details.
Get Gugulethu Nyoni’s stories in your inbox
Join Medium for free to get updates from this writer.
Let’s take a closer look at how to access and modify nested objects.
1. Accessing Nested Properties
You can access nested properties using dot notation or bracket notation.
// Accessing nested properties
console.log(userProfile.preferences.theme); // "dark"
console.log(userProfile["preferences"]["notifications"]); // true2. Modifying Nested Properties
You can update values in a nested object by targeting the specific key.
// Updating a nested property
userProfile.preferences.theme = "light";
console.log(userProfile.preferences.theme); // "light"3. Adding New Nested Properties
You can also add new properties to nested objects.
// Adding a new nested property
userProfile.preferences.language = "English";
console.log(userProfile.preferences.language); // "English"4. Deleting Nested Properties
Deleting a property from a nested object is as simple as using the `delete` operator on the nested key.
// Deleting a nested property
delete userProfile.preferences.notifications;
console.log(userProfile.preferences.notifications); // undefinedPractical Examples
Let’s now look at a practical example where we create an object to store user preferences, and then manipulate it as needed.
Example: Managing User Preferences
// Initial user preferences
const userPrefs = {
theme: "dark",
language: "English",
notifications: true
};
// Updating preferences
userPrefs.language = "Spanish"; // Change language
userPrefs.notifications = false; // Turn off notifications
// Adding a new preference
userPrefs.location = "USA"; // Add location
// Deleting an old preference
delete userPrefs.notifications; // Remove notifications preference
console.log(userPrefs);Output:
{
theme: "dark",
language: "Spanish",
location: "USA"
}In this example, we’ve created an object called `userPrefs` to store various user preferences. We then update some properties, add a new one, and delete an old one.
Common Pitfalls with Objects
1. Direct Mutation of Nested Objects
One of the most common mistakes when working with nested objects is mutating them directly. This can lead to unintended side effects, especially when passing objects around in your application.
Solution: Use techniques like spread syntax or libraries like `Immer` to ensure you don’t mutate objects unintentionally.
// Direct mutation (BAD practice)
userProfile.preferences.theme = "light";
// Proper method (GOOD practice using spread)
const updatedProfile = {
…userProfile,
preferences: {
…userProfile.preferences,
theme: "light"
}
};2. Shallow Copy vs. Deep Copy
When copying objects that contain nested structures, you may encounter issues with shallow copies. In a shallow copy, nested objects aren’t duplicated but merely referenced. This can lead to unexpected side effects if you modify the copied object, as changes to nested properties will affect the original object too.
Example:
const shallowCopy = { ...userProfile };
shallowCopy.preferences.theme = "light";
console.log(userProfile.preferences.theme); // Also "light" — unexpected!Solution:
To avoid this, use deep cloning, which creates an entirely independent copy of an object, including all nested properties.
The recommended modern way to do this is with the built-in structuredClone() function:
const deepCopy = structuredClone(userProfile);This method safely clones complex objects, handling nested structures, Dates, Maps, Sets, and circular references properly.
Alternatively, for older environments or simpler data structures, you might still see the JSON.parse(JSON.stringify(object)) approach, though it has limitations (e.g., it doesn’t handle Dates, undefined, or circular references).
You can also use specialized libraries like Lodash’s cloneDeep if you need support for older browsers or additional flexibility.
Thanks to Jon Randy Jon for the helpful suggestion on using structuredClone() — a cleaner and safer built-in option for modern JavaScript!
Conclusion
By mastering JavaScript objects, you’re now equipped to store and manage data efficiently, manipulate user profiles, configurations, and more complex structures in your applications. This knowledge will serve as a solid foundation for building scalable and maintainable applications.
Up Next: Combining Arrays and Objects: Advanced Data Structures in Action
What’s the most complex thing you have had to do with objects or the most most complex object you’ve ever worked with in JavaScript? How did you handle nested data? Share your thoughts below!










