Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Property accessors in JavaScript

Dot notation names a fixed key; bracket notation computes one. Either form can read stored data or trigger a getter or setter, depending on the property.

Version: ECMAScript 2024 and the current web platform; runtime support varies

Syntax

obj.name
obj[expression]
obj.name = value
obj[expression] = value
obj?.name
obj?.[expression]

({
  get name() { return this.saved; },
  set name(value) { this.saved = value; }
})

Property accessors are the syntax for referring to properties. obj.name uses the string key "name", regardless of whether a variable called name exists. obj[key] evaluates the expression in brackets and converts its result to a string or Symbol key. Use brackets for dynamic keys, symbols, or names such as obj["first-name"]. Access to a private class element via obj.#name is separate: obj["#name"] is not equivalent.

On an ordinary object, a read checks the object's own property first, then follows the prototype chain. If no property is found, it returns undefined. A data property stores a value; an accessor property instead can define a getter (get) or a setter (set). A read calls the getter with no arguments, while assignment passes the assigned value to the setter. Assigning to an accessor with no setter throws TypeError in strict mode and is ignored otherwise.

An ordinary read from null or undefined throws TypeError. obj?.name and obj?.[key] return undefined if obj is either of those values. In that case, the key expression is not evaluated. ?. does not catch exceptions thrown by a getter and cannot be used on the left-hand side of an assignment.

Examples

{
  const key = 'value';
  const label = Symbol('label');
  const counter = {
    count: 2,
    [label]: 'counter',
    get value() { return this.count; },
    set value(next) { this.count = next; }
  };

  console.log(`${counter[label]}: ${counter.value}`);
  counter[key] = 5;
  console.log(`${counter.count} / ${counter[key]}`);

  const missing = null;
  console.log(String(missing?.[key]));
}

Result

counter: 2
5 / 5
undefined

`counter[label]` reads a property keyed by a `Symbol`. Both `counter.value` and `counter[key]` call the `value` getter; the bracket assignment calls the setter, which updates `count`. The last read short-circuits at `null`.

Related lessons

  • JavaScript · Lesson 1 · Advanced · Version: 4
  • JavaScript · Lesson 2 · Advanced · Version: 4
  • JavaScript · Lesson 3 · Advanced · Version: 4
  • JavaScript · Lesson 4 · Advanced · Version: 4
  • JavaScript · Lesson 5 · Advanced · Version: 4
  • JavaScript · Lesson 6 · Advanced · Version: 4
  • JavaScript · Lesson 7 · Advanced · Version: 4
  • JavaScript · Lesson 8 · Advanced · Version: 4
  • JavaScript · Lesson 9 · Advanced · Version: 4
  • JavaScript · Lesson 10 · Advanced · Version: 4
  • JavaScript · Lesson 11 · Advanced · Version: 4
  • JavaScript · Lesson 12 · Advanced · Version: 4
  • JavaScript · Lesson 13 · Advanced · Version: 4
  • JavaScript · Lesson 14 · Advanced · Version: 4
  • JavaScript · Lesson 15 · Advanced · Version: 4
  • JavaScript · Lesson 16 · Advanced · Version: 4
  • JavaScript · Lesson 17 · Advanced · Version: 4
  • JavaScript · Lesson 18 · Advanced · Version: 4
  • JavaScript · Lesson 19 · Advanced · Version: 4
  • JavaScript · Lesson 12 · Intermediate · Version: 3
  • JavaScript · Lesson 13 · Intermediate · Version: 3
  • JavaScript · Lesson 14 · Intermediate · Version: 3
  • JavaScript · Lesson 15 · Intermediate · Version: 3
  • JavaScript · Lesson 16 · Intermediate · Version: 3
  • JavaScript · Lesson 17 · Intermediate · Version: 3
  • JavaScript · Lesson 18 · Intermediate · Version: 3
  • JavaScript · Lesson 19 · Intermediate · Version: 3
  • JavaScript · Lesson 20 · Intermediate · Version: 3
  • JavaScript · Lesson 21 · Intermediate · Version: 3
  • JavaScript · Lesson 22 · Intermediate · Version: 3
  • JavaScript · Lesson 23 · Intermediate · Version: 3
  • JavaScript · Lesson 24 · Intermediate · Version: 3
  • JavaScript · Lesson 25 · Intermediate · Version: 3
  • JavaScript · Lesson 26 · Intermediate · Version: 3
  • JavaScript · Lesson 27 · Intermediate · Version: 3
  • JavaScript · Lesson 28 · Intermediate · Version: 3
  • JavaScript · Lesson 29 · Intermediate · Version: 3
  • JavaScript · Lesson 30 · Intermediate · Version: 3
  • JavaScript · Lesson 31 · Intermediate · Version: 3
  • JavaScript · Lesson 32 · Intermediate · Version: 3
  • JavaScript · Lesson 33 · Intermediate · Version: 3
  • JavaScript · Lesson 34 · Intermediate · Version: 3
  • JavaScript · Lesson 35 · Intermediate · Version: 3
  • JavaScript · Lesson 36 · Intermediate · Version: 3
  • JavaScript · Lesson 37 · Intermediate · Version: 3
  • JavaScript · Lesson 38 · Intermediate · Version: 3
  • JavaScript · Lesson 39 · Intermediate · Version: 3
  • JavaScript · Lesson 40 · Intermediate · Version: 3
  • JavaScript · Lesson 41 · Intermediate · Version: 3
  • JavaScript · Lesson 42 · Intermediate · Version: 3
  • JavaScript · Lesson 43 · Intermediate · Version: 3

Sources