JavaScriptArrays & Objects
JavaScript Objects
Learn JavaScript objects, how to create them, access and modify properties, and use objects to represent real-world entities in applications.
What is an Object in JavaScript?Link to this section
An object is a collection of related data and functionality stored as key–value pairs. Objects are used to represent real-world entities such as users, products, orders, and configurations.
Objects help structure data in a meaningful and organized way.
Why Use Objects?Link to this section
Why Use Objects?
- Group related data together
- Represent real-world entities
- Improve code clarity and maintainability
- Work efficiently with APIs and databases
Creating an Object :Link to this section
Objects are created using curly braces {}.
note
Keys are also called properties, and values can be of any data type.
Accessing Object Properties :Link to this section
There are two ways to access object properties.
Dot Notation
Code Title: Dot Notation Example ```javascript console.log(user.name); ```Bracket Notation
Code Title: Bracket Notation Example ```javascript console.log(user["age"]); ```tip
Bracket notation is useful when property names are dynamic or stored in variables.
Modifying Object Properties :Link to this section
Object properties can be updated or new properties can be added at any time.
Deleting Object Properties :Link to this section
The delete keyword removes a property from an object.
warning
Deleting properties can affect application logic if not handled carefully.
Objects with Methods :Link to this section
Objects can also contain functions, called methods.
Explanation:
The this keyword refers to the current object.
Nested Objects :Link to this section
Objects can contain other objects as values.
Objects in Real-World Scenarios :Link to this section
Objects are commonly used for:
- User profiles
- Product details
- API responses
- Application configuration
tip
Think of objects as nouns in your application and functions as verbs.
Check Your Understanding
Question 1 of 4What does an object store?
Create an Object
Create an object to store details of a book (title, author, price)..
Access Properties
Print the author name from the book object..