Dot notation and computed property access
Quick answer
Dot notation reads a property with a fixed name. Bracket notation evaluates an expression to determine the property key.
How it works
record.price is equivalent to record["price"]. With a variable key, use record[key]. Missing properties ordinarily yield undefined unless inherited; reading a property of null or undefined throws.
const item = { price: 12, "unit label": "kg" };console.log(item.price, item["unit label"]);Expected output in Node.js:
12 kgBracket notation handles names with spaces.
const scores = { math: 8, art: 9 };const key = "art";console.log(scores[key], scores.key);Expected output in Node.js:
9 undefinedBracket notation evaluates key; dot notation asks for the literal name key.
Common mistakes
A property lookup is not an own-property existence check. Do not assume missing fields always produce undefined if prototypes supply a value.
Practice
Use a variable to select the price or quantity from a product.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Dot notation and computed property access — Knowledge Check
Apply the taught concept and reason about the result.
What does const o={price:8}; o["price"] evaluate to?
Given const key="art"; const o={art:9}; which reads 9 using key?
Which notation can directly access a key containing a space?
What happens when reading null.price?
Does reading a property tell you whether it is an own property?