DevAcademy
LearnJavaScriptJavaScript Objects
BeginnerJavaScript

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

Try it yourself — edit and run

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

Try it yourself — edit and run

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

Try it yourself — edit and run

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

Try it yourself — edit and run

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

Try it yourself — edit and run

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

Try it yourself — edit and run

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

Try it yourself — edit and run

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

Try it yourself — edit and run

Console Output

Click “Run” to see the console output here.

Useful Object Methods

MethodDescription
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.

Interview Questions

Quick Quiz

1. How are object properties stored?

2. Which notation is useful for dynamic property names?

3. Which method returns all property names?