Object literals and named properties
Quick answer
An object literal groups related values under named property keys. Unlike an array index, a descriptive key identifies a field by its purpose.
How it works
Use { name: "Ada", score: 10 }. Keys are strings or symbols; ordinary numeric keys become strings. A shorthand such as { score } uses the value of the binding named score.
const book = { title: "Atlas", pages: 120 };console.log(book.title, book.pages);Expected output in Node.js:
Atlas 120Each field has a name and a value.
const quantity = 3;const item = { quantity, price: 8 };console.log(item.quantity * item.price);Expected output in Node.js:
24The shorthand creates the quantity property from the existing binding.
Common mistakes
An object literal is not JSON: unquoted identifier keys and shorthand are allowed here. JSON has different syntax rules, taught later.
Practice
Represent a product with a name, quantity and unit price; compute its total.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Object literals and named properties — Knowledge Check
Apply the taught concept and reason about the result.
Which expression represents a record with a named stock field?
What does const quantity=3; const o={quantity}; produce for o.quantity?
Can an object literal contain an array-valued property?
What happens to an ordinary numeric property key in an object?
Why is {name:"Ada"} in an expression not strict JSON syntax?