Keep related facts together
A product has a name, price and category. Three parallel arrays can drift out of alignment when one is changed. An object groups named properties belonging to one entity.
const product = {
name: "Notebook",
price: 20,
category: "stationery"
};
console.assert(product.name === "Notebook");
console.assert(product.price === 20);
console.assert(product.category === "stationery");
An object literal uses braces. Each property pairs a key with a value; commas separate properties. Dot notation reads a named property. Values can be primitive values, arrays or other objects.
Design before adding fields
What does price mean: units or minor units, before or after discount? The property name alone cannot settle that policy. For this lesson it means a nonnegative price in whole units before any charges. Later projects use integer minor units and explicit rounding.
Objects are not automatically database records. This object exists in memory during the program and disappears when that context is discarded.
Guided comparison
Putting a customer's phone number and a product price into one vague object called data does not create a useful model. Group facts that belong to the same concept; choose names that reveal it.
Independent exercise
Model a collection order with a reference, customer name and collected status. Choose suitable primitive types. Explain why a reference of "0009" should remain text.
Correction
An object can contain reference: "0009", customerName: "Sara" and collected: false. The reference identifies the order; leading zeros may matter, and it is not an arithmetic quantity. Use a Boolean for collected, not the text "false".
Review
Before manipulating a record, write what each property means and what is required. Consistent shapes allow the next functions to rely on clear inputs rather than guessing which spelling or representation is present.