JavaScript Objects
Learn how JavaScript objects work, how to create them, access properties, add methods, and work with nested objects.
Reading Time
18 min
Lesson
Lesson 14 of 48
What is an Object?
An object is a collection of related data stored as key-value pairs. Objects help represent real-world entities like a user, product, or car by grouping related information together.
Creating an Object
Console Output
Click “Run” to see the console output here.
Accessing Object Properties
Object properties can be accessed using dot notation or bracket notation.
Dot vs Bracket Notation
Console Output
Click “Run” to see the console output here.
Updating Properties
You can update an existing property by assigning a new value to it.
Update Object Property
Console Output
Click “Run” to see the console output here.
Adding New Properties
JavaScript objects are dynamic, meaning new properties can be added at any time.
Add New Property
Console Output
Click “Run” to see the console output here.
Deleting Properties
Use the delete operator to remove a property from an object.
Delete Property
Console Output
Click “Run” to see the console output here.
Nested Objects
Objects can contain other objects, allowing you to represent complex data structures.
Nested Object Example
Console Output
Click “Run” to see the console output here.
Object Methods
Functions stored inside objects are called methods. They allow objects to perform actions.
Creating a Method
Console Output
Click “Run” to see the console output here.
Looping Through Objects
Use the for...in loop to iterate through an object's properties.
for...in Loop
Console Output
Click “Run” to see the console output here.
Useful Object Methods
| Method | Description |
|---|---|
| Object.keys() | Returns an array of property names |
| Object.values() | Returns an array of property values |
| Object.entries() | Returns key-value pairs as arrays |
| Object.assign() | Copies properties from one object to another |
| hasOwnProperty() | Checks if a property exists |
Remember
Objects are reference types. Assigning one object to another variable copies the reference, not the actual object.