0
0
Javascriptprogramming~5 mins

Object use cases in Javascript

Choose your learning style9 modes available
Introduction

Objects help us group related information together in one place. They make it easy to organize and use data like real-world things.

To store details about a person like name, age, and address.
To keep settings or preferences for an app in one place.
To represent items in a game with properties like health and score.
To group related functions and data for a feature.
To collect data from a form before sending it somewhere.
Syntax
Javascript
const objectName = {
  key1: value1,
  key2: value2,
  key3: value3
};

Keys are like labels for values inside the object.

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

Examples
This object stores a person's name and age.
Javascript
const person = {
  name: "Alice",
  age: 30
};
This object holds app settings like theme and notifications.
Javascript
const settings = {
  theme: "dark",
  notifications: true
};
This object represents a game item with its properties.
Javascript
const gameItem = {
  type: "sword",
  damage: 10,
  durability: 100
};
Sample Program

This program creates a car object with details and a honk function. It then prints the car info and the honk sound.

Javascript
const car = {
  brand: "Toyota",
  model: "Corolla",
  year: 2020,
  honk() {
    return "Beep beep!";
  }
};

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

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

Objects help keep code organized and easier to understand.

Summary

Objects group related data with labels (keys) and values.

They are useful for representing real-world things and settings.

Objects can also have functions to do actions related to their data.