Get to know MDN better
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
The get syntax binds an object property to a function that will be called when that property is looked up. It can also be used in classes.
There are some additional syntax restrictions:
The name of the property to bind to the given function. In the same way as other properties in object initializers, it can be a string literal, a number literal, or an identifier.
expressionYou can also use expressions for a computed property name to bind to the given function.
Sometimes, it is desirable to allow access to a property that returns a dynamically computed value, or you may want to reflect the status of an internal variable without requiring the use of explicit method calls. In JavaScript, this can be accomplished with the use of a getter.
An object property is either a data property or an accessor property, but it cannot simultaneously be both. Read Object.defineProperty() for more information. The getter syntax allows you to specify the getter function in an object initializer.
Properties defined using this syntax are own properties of the created object, and they are configurable and enumerable.
This will create a pseudo-property latest for object obj, which will return the last array item in log.
Note that attempting to assign a value to latest will not change it.
You can use the exact same syntax to define public instance getters that are available on class instances. In classes, you don't need the comma separator between methods.
Getter properties are defined on the prototype property of the class and are thus shared by all instances of the class. Unlike getter properties in object literals, getter properties in classes are not enumerable.
Static getters and private getters use similar syntaxes, which are described in the static and private elements pages.
If you want to remove the getter, you can just delete it:
To append a getter to an existing object later at any time, use Object.defineProperty().
Getters give you a way to define a property of an object, but they do not calculate the property's value until it is accessed. A getter defers the cost of calculating the value until the value is needed. If it is never needed, you never pay the cost.
An additional optimization technique to lazify or delay the calculation of a property value and cache it for later access are smart (or memoized) getters. The value is calculated the first time the getter is called and is then cached so subsequent accesses return the cached value without recalculating it. This is useful in the following situations:
Note: This means that you shouldn't write a lazy getter for a property whose value you expect to change, because if the getter is lazy, then it will not recalculate the value.
Note that getters are not "lazy" or "memoized" by nature; you must implement this technique if you desire this behavior.
In the following example, the object has a getter as its own property. On getting the property, the property is removed from the object and re-added, but implicitly as a data property this time. Finally, the value gets returned.
Many functions accept an object and retrieve individual properties from it as separate parameters (this object parameter is known as an options bag). You can detect whether a specific option is supported by using a getter to track if the property has been retrieved. This example checks if the colorType option is supported by the HTMLCanvasElement.getContext() method.
While using the get keyword and Object.defineProperty() have similar results, there is a subtle difference between the two when used on classes. The get syntax defines the property on the instance's prototype, while Object.defineProperty() defines the property on the instance it is applied to.
| ECMAScript® 2027 Language Specification # sec-method-definitions |
Enable JavaScript to view this browser compatibility table.
This page was last modified on Oct 31, 2025 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.