property accessors (., [])
In JavaScript, the property accessors (., []) read or write a property of an object by its key. Dot notation takes a fixed name written in the code, while bracket notation evaluates an expression and uses the result as the key.
Bracket notation converts any key that isn’t a string or a symbol to a string, so votes[3] and votes["3"] reach the same property. A missing property reads as undefined, but any access on null or undefined throws a TypeError. The optional chaining operator (?.) stops early on those two values instead.
JavaScript Property Accessors Syntax
Dot notation needs a valid identifier after the dot, and bracket notation accepts any expression:
<object>.<identifier>
<object>[<expression>]
The two accessors give these results for const user = { name: "Ada" };:
| Operator | Name | Example | Result |
|---|---|---|---|
. |
Dot notation | user.name |
"Ada" |
[] |
Bracket notation | user["name"] |
"Ada" |
The identifier after the dot can be a reserved word, so user.default is valid. The same square brackets also form a computed key in an object literal, { [key]: value }.
JavaScript Property Accessors Examples
Say an order-tracking page shows whichever field a user picks from a menu. The field name arrives as a string, so bracket notation reads it:
> const order = { id: 1042, status: "shipped", "ship-to": "Lisbon" };
undefined
> const field = "status";
undefined
> order[field]
'shipped'
> order.field
undefined
> order["ship-to"]
'Lisbon'
The expression order[field] evaluates field and reads order.status. The dot form order.field looks for a property literally named field and finds none. The key "ship-to" contains a hyphen, so only bracket notation can reach it.
Say a poll tallies votes by numeric option IDs. Numbers used as keys become strings, and a computed key builds a property from a variable:
> const votes = {};
undefined
> votes[3] = 1;
1
> votes["3"] += 1;
2
> Object.keys(votes)
[ '3' ]
> const choice = "remote";
undefined
> ({ [choice]: true })
{ remote: true }
Both writes update the single key "3", which Object.keys() lists as a string.
JavaScript Property Accessors Gotchas
- Reading a property of
nullorundefinedthrows, soorder.customer.cityfails with aTypeErrorwhenorder.customeris missing. - Bracket notation turns a plain object used as a key into the string
"[object Object]", so two different objects used as keys share one property. AMapkeeps object keys apart. - A dot right after an integer literal reads as a decimal point, so
5.toFixed(2)is aSyntaxError, while(5).toFixed(2)returns"5.00".
JavaScript Property Accessors Use Cases
- Reading and writing fields whose names are fixed in the code
- Looking up a key that’s only known at runtime, such as a user’s choice
- Accessing keys that aren’t valid identifiers, such as
"content-type" - Building objects whose keys come from variables
- Counting or grouping values under string keys
Related Resources
Course
Records With Objects
Describe one thing with named fields in a JavaScript object, keep a list of those records, and write functions that select, find by id and update them. Build a board-game shelf helper, then a small records program of your own.
For additional information on related topics, take a look at the following resources:
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.
By Leodanis Pozo Ramos • Updated Nov. 12, 2026