You’re reading the English version of this content since no translation exists yet for this locale. Help us translate this article!

The set syntax binds an object property to a function to be called when there is an attempt to set that property.


{set prop(val) { . . . }}
{set [expression](val) { . . . }}


The name of the property to bind to the given function.
An alias for the variable that holds the value attempted to be assigned to prop.
Starting with ECMAScript 2015, you 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 a specified property is attempted to be changed. Setters are most often used in conjunction with getters to create a type of pseudo-property. It is not possible to simultaneously have a setter on a property that holds an actual value.

Note the following when working with the set syntax:


Defining a setter on new objects in object initializers

This will define a pseudo-property current of object language that, when assigned a value, will update log with that value:

var language = {
  set current(name) {
  log: []

language.current = 'EN';
console.log(language.log); // ['EN']

language.current = 'FA';
console.log(language.log); // ['EN', 'FA']

Note that current is not defined and any attempts to access it will result in undefined.

Removing a setter with the delete operator

If you want to remove the setter, you can just delete it:

delete o.current;

Defining a setter on existing objects using defineProperty

To append a setter to an existing object later at any time, use Object.defineProperty().

var o = {a: 0};

Object.defineProperty(o, 'b', { set: function(x) { this.a = x / 2; } });

o.b = 10; // Runs the setter, which assigns 10 / 2 (5) to the 'a' property
console.log(o.a) // 5

Using a computed property name

var expr = 'foo';

var obj = {
  baz: 'bar',
  set [expr](v) { this.baz = v; }

console.log(obj.baz); // "bar" = 'baz';      // run the setter
console.log(obj.baz); // "baz"


Specification Status Comment
ECMAScript 5.1 (ECMA-262)
The definition of 'Object Initializer' in that specification.
Standard Initial definition.
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'Method definitions' in that specification.
Standard Added computed property names.
ECMAScript Latest Draft (ECMA-262)
The definition of 'Method definitions' in that specification.

Browser compatibility

Update compatibility data on GitHub
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
setChrome Full support 1Edge Full support YesFirefox Full support 2IE Full support 9Opera Full support 9.5Safari Full support 3WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
Computed property namesChrome Full support 46Edge Full support YesFirefox Full support 34IE No support NoOpera Full support 47Safari No support NoWebView Android Full support 46Chrome Android Full support 46Firefox Android Full support 34Opera Android Full support YesSafari iOS No support NoSamsung Internet Android Full support 5.0nodejs Full support Yes


Full support  
Full support
No support  
No support

See also