Skip to content

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:

Language: JavaScript Syntax
<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:

Language: JavaScript
> 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:

Language: JavaScript
> 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 null or undefined throws, so order.customer.city fails with a TypeError when order.customer is 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. A Map keeps object keys apart.
  • A dot right after an integer literal reads as a decimal point, so 5.toFixed(2) is a SyntaxError, 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
A developer turns dials on a console with a card reading Title, Players and Minutes while a robot arm lifts a board game from a shelf onto a conveyor marked Find by ID beside an Update button.

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.

basics js

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