JavaScript set()

0
12K

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.

Examples

 

Defining a setter on new objects in object initializers

The following example define a pseudo-property current of object language. When current is assigned a value, it updates log with that value:

const language = {
  set current(name) {
    this.log.push(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.

Buscar
Categorías
Read More
Marketing and Advertising
How Do I Start Online Advertising? A Step-by-Step Guide
Starting online advertising can feel overwhelming. With so many platforms, formats, metrics, and...
By Dacey Rankins 2026-01-28 16:00:59 0 4K
Productivity
What is work-life balance?
What Is Work-Life Balance? Work-life balance is a concept that has gained significant attention...
By Michael Pokrovski 2026-03-25 18:48:56 0 3K
Economics
What Is Elasticity in Economics? Price Elasticity of Demand and Supply
What Is Elasticity in Economics?Price Elasticity of Demand and Supply In economics, people often...
By Leonard Pokrovski 2026-01-31 22:14:06 0 7K
Productivity
Why do I never achieve my goals?
The Anatomy of the Unfinished It is a common frustration: the initial surge of inspiration,...
By Michael Pokrovski 2026-05-06 19:44:31 0 2K
Economics
Why is environmental economics important?
Why Environmental Economics Matters More Than Most Economists Admit The modern economist suffers...
By Leonard Pokrovski 2026-05-21 19:31:41 0 4K

BigMoney.VIP Powered by Hosting Pokrov