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 set syntax binds an object property to a function to be called when there is an attempt to set that property. 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.
valAn alias for the variable that holds the value attempted to be assigned to prop.
expressionYou can also use expressions for a computed property name to bind to the given function.
In JavaScript, a setter can be used to execute a function whenever an attempt is made to change a property's value. Setters are most often used in conjunction with getters.
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 setter syntax allows you to specify the setter function in an object initializer.
Properties defined using this syntax are own properties of the created object, and they are configurable and enumerable.
The following example defines a pseudo-property current of object language. When current is assigned a value, it updates log with that value:
Note that current is not defined, and any attempts to access it will result in undefined.
You can use the exact same syntax to define public instance setters that are available on class instances. In classes, you don't need the comma separator between methods.
Setter properties are defined on the prototype property of the class and are thus shared by all instances of the class. Unlike setter properties in object literals, setter properties in classes are not enumerable.
Static setters and private setters use similar syntaxes, which are described in the static and private elements pages.
If you want to remove the setter, you can just delete it:
To append a setter to an existing object, use Object.defineProperty().
| ECMAScript® 2027 Language Specification # sec-method-definitions |
Enable JavaScript to view this browser compatibility table.
This page was last modified on Jul 8, 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.