0
0
Javascriptprogramming~5 mins

Why objects are needed in Javascript

Choose your learning style9 modes available
Introduction

Objects help us group related information together in one place. They make it easier to organize and use data that belongs to the same thing.

When you want to store details about a person like name, age, and address.
When you need to keep track of a product's price, name, and quantity in a store.
When you want to group settings or options for a game or app.
When you want to represent a real-world thing with many properties in your code.
Syntax
Javascript
const objectName = {
  key1: value1,
  key2: value2,
  key3: value3
};

Keys are names that describe the data.

Values can be numbers, strings, arrays, or even other objects.

Examples
This object stores a person's name and age.
Javascript
const person = {
  name: "Alice",
  age: 30
};
This object holds details about a product.
Javascript
const product = {
  name: "Book",
  price: 9.99,
  inStock: true
};
Sample Program

This program creates an object to store car details and then prints each detail.

Javascript
const car = {
  brand: "Toyota",
  model: "Corolla",
  year: 2020
};

console.log(`Car brand: ${car.brand}`);
console.log(`Model: ${car.model}`);
console.log(`Year: ${car.year}`);
OutputSuccess
Important Notes

Objects help keep related data together, making code easier to read and manage.

You can change object values anytime by assigning new values to keys.

Summary

Objects group related data under one name.

They make it easy to organize and access information.

Objects are useful for representing real-world things in code.