Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

get — property getter

The get syntax defines a getter: a function invoked when a property is read. It lets you compute a value without calling a method with parentheses; get itself is not a reserved word.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

const obj = { get propertyName() { return expression; } };

get name() { ... } defines a getter in an object literal or class. Reading obj.name runs its body; do not write obj.name() unless the returned value is a function. A getter cannot declare parameters.

get has contextual meaning here, but is not a reserved word: const get = 1 is valid. name is an accessor property: its descriptor holds the getter function, not the value it returns.

Each read runs the getter again—the result is not automatically cached. Do not read this.name inside the getter for name, because that causes recursion.

Without a matching set setter, assignment to the property throws TypeError in strict mode and is ignored otherwise. This does not make the entire object immutable.

Examples

"use strict";
const cart = {
  price: 12,
  quantity: 3,
  get total() {
    return this.price * this.quantity;
  }
};

console.log(cart.total);
cart.quantity = 4;
console.log(cart.total);

Result

36
48

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources