Understanding Objects in JavaScript

What Objects Are and Why They Are Needed
A single variable can hold one value: a name, an age, a city. But real-world things rarely have just one property — a person has a name and an age and a city, all belonging together as one unit. Tracking these as separate, disconnected variables quickly becomes unwieldy:
const personName = "Aarav";
const personAge = 21;
const personCity = "Delhi";An object solves this by grouping related data together, as a single value made up of key-value pairs:
const person = {
name: "Aarav",
age: 21,
city: "Delhi",
};Each key (name, age, city) is a label; each corresponding value ("Aarav", 21, "Delhi") is the actual data. Together, person represents one coherent thing, instead of three scattered variables that happen to be related only by convention.

Creating Objects
The most common way to create an object is with object literal syntax — curly braces containing key-value pairs:
const student = {
name: "Priya",
age: 20,
course: "Computer Science",
};Each key is followed by a colon, then its value, with commas separating the pairs. Property keys in object literals are commonly written without quotes when they are valid identifier names. Quotes are required when the key contains spaces, hyphens, or other characters that cannot be used in an unquoted property name. Property keys can also be written using computed expressions.
const key = "course";
const student = {
name: "Priya",
[key]: "Computer Science",
};
console.log(student.course);
// "Computer Science"Here, [key] is a computed property name. JavaScript evaluates key and uses its value ("course") as the property name.

Accessing Properties: Dot Notation and Bracket Notation
There are two ways to read a value out of an object.
Dot notation
console.log(student.name); // "Priya"
console.log(student.age); // 20This is the most common, most readable form — used when the property name is known in advance and can be written as a valid identifier name after the dot (for example, no spaces or hyphens).
Bracket notation
console.log(student["name"]); // "Priya"
console.log(student["age"]); // 20Bracket notation becomes necessary in two common situations:
When the key is stored in a variable:
const key = "course";
console.log(student[key]); // "Computer Science" — dot notation can't do thisWhen the key contains characters dot notation can’t handle, like a space:
const info = { "full name": "Priya Sharma" };
console.log(info["full name"]); // dot notation (info.full name) would be invalid syntax



