- Courses
- 💻Programming Languages
- 🌐JavaScript
- Objects and Classes
- Objects
On this page
JavaScript Objects
By Enrico Hochkoeppler
Web Developer & TutoriaLibre Founder
In JavaScript, an object is a complex data type used to store collections of data and more complex entities. While primitive data types like strings or numbers represent a single value, an object allows you to group related data together in a structured format using key-value pairs. This makes objects essential for representing real-world items, such as a user profile, a product in a store, or a configuration setting.
Object Structure and Key-Value Pairs
An object is defined using curly braces {}. Inside these braces, data is organized into pairs. Each pair consists of a key (also known as a property name) and a value.
- Key: A unique identifier (string or symbol) that acts as a label for the data.
- Value: The data assigned to that key, which can be any JavaScript data type—including strings, numbers, booleans, arrays, or even other objects.
const car = {
make: "Toyota",
model: "Camry",
year: 2024,
isElectric: false,
};
In this example, make is a key, and "Toyota" is its corresponding value. Each pair is separated by a comma.
Accessing Properties
Once an object is created, you need a way to retrieve its data. JavaScript provides two primary methods for property access: Dot Notation and Bracket Notation.
Dot Notation
Dot notation is the most common and readable way to access object properties. You append a dot (.) followed by the property name to the object variable.
console.log(car.make); // Output: Toyota
console.log(car.year); // Output: 2024
- Constraint: You can only use dot notation when the property name is a valid identifier (it cannot contain spaces, start with a number, or include special characters).
Bracket Notation
Bracket notation involves placing the property name as a string inside square brackets []. This method is more flexible than dot notation.
console.log(car["model"]); // Output: Camry
- Requirement: The key inside the brackets must be a string (wrapped in quotes) unless you are using a variable.
When to Use Bracket Notation Over Dot Notation
While dot notation is preferred for its simplicity, bracket notation is required in several specific professional scenarios.
Using Dynamic Keys (Variables)
If the name of the property you want to access is stored in a variable, you must use bracket notation. Dot notation will look for a literal property with the name of that variable.
const propertyToRead = "make";
console.log(car.propertyToRead); // undefined (looks for a key named 'propertyToRead')
console.log(car[propertyToRead]); // Output: Toyota (evaluates the variable)
Accessing Non-Standard Keys
If a property key contains spaces, hyphens, or starts with a number, dot notation will result in a syntax error. Bracket notation handles these strings easily.
const user = {
"first name": "John",
"account-type": "Premium",
};
// console.log(user.first name); // Syntax Error
console.log(user["first name"]); // Output: John
Modifying and Adding Properties
Objects in JavaScript are mutable, meaning you can change their contents after they are created. You can use either notation to update existing values or create entirely new properties.
const laptop = {
brand: "Dell",
ram: 8,
};
// Updating an existing property
laptop.ram = 16;
// Adding a new property
laptop.color = "Silver";
console.log(laptop); // { brand: "Dell", ram: 16, color: "Silver" }
Practical Implementation Principles
To ensure your code remains maintainable and efficient, follow these industry standards:
- Default to Dot Notation: Use dot notation for its brevity and readability whenever property names are known and follow standard naming rules.
- Use Bracket Notation for Logic: Reserve bracket notation for cases involving dynamic data, variables, or keys that do not follow standard naming conventions.
- Consistent Naming: While JavaScript allows strings with spaces as keys, it is best practice to use camelCase for object keys (e.g.,
userEmailinstead of"user-email") to allow for consistent use of dot notation.
Found this helpful? Share the knowledge...
... or support this project to keep it alive!
Previous
Objects and Classes
Next
this Keyword
-
Object Structure and Key-Value Pairs
-
Accessing Properties
- Dot Notation
- Bracket Notation
-
When to Use Bracket Notation Over Dot Notation
- Using Dynamic Keys (Variables)
- Accessing Non-Standard Keys
-
Modifying and Adding Properties
-
Practical Implementation Principles
