get
In JavaScript, get is a contextual keyword that defines a getter, a function that runs whenever code reads a property. It acts as a keyword only in front of a method name inside a class body or an object literal, and stays a valid identifier everywhere else.
A getter takes no parameters, and reading the property returns whatever the function returns. It’s usually paired with a set accessor of the same name. A getter in an object literal is an own, enumerable property. A getter in a class lives on the class’s .prototype object and isn’t enumerable, while one marked static lives on the class itself. Getters don’t cache their result, so the function runs again on every read.
Getters in object literals date from ES5, and class getters from ES2015.
JavaScript get Keyword Syntax
A getter can be defined in an object literal or in a class:
const <object> = {
get <name>() {
<statements>
},
};
class <Name> {
[static] get <name>() {
<statements>
}
}
The name can also be a string, a number, a computed [expression], or a private #name in a class.
JavaScript get Keyword Examples
A getter computes the rectangle’s area each time it’s read:
> const rectangle = {
… width: 4,
… height: 3,
… get area() {
… return this.width * this.height;
… },
… };
undefined
> rectangle.area
12
> rectangle.width = 10;
10
> rectangle.area
30
> rectangle.area = 0;
0
> rectangle.area
30
Reading .area calls the getter, so the result follows the current width. Without a setter, the assignment has no effect in sloppy mode, and throws a TypeError in strict mode.
Outside an accessor definition, get is an ordinary name:
> const get = (key) => key.toUpperCase();
undefined
> get("id")
'ID'
JavaScript get Keyword Use Cases
- Computing a property from other properties when it’s read
- Exposing a private field as a read-only property
- Validating or formatting a value on the way out
- Keeping a property name stable while its implementation changes
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. 4, 2026