Reflect.set
Sets the value of a property on an object, similar to target[propertyKey] = value but as a function
Syntax
Reflect.set(target, propertyKey, value, receiver?)Parameters
| Parameter | Type | Description |
|---|---|---|
| target | object | The target object to set the property on |
| propertyKey | string | symbol | The property name |
| value | any | The value to set |
| receiver | any | The value of this for setters |
Return Value
A boolean indicating whether setting was successful
Examples
const obj: Record<string, number> = {}
Reflect.set(obj, 'x', 42)
console.log(obj.x) // 42const handler: ProxyHandler<Record<string, unknown>> = {
set(target, prop, value, receiver) {
console.log(`Setting ${String(prop)} = ${value}`)
return Reflect.set(target, prop, value, receiver)
}
}const frozen = Object.freeze({ a: 1 })
console.log(Reflect.set(frozen, 'a', 2)) // false (read-only)Understanding Reflect.set
The Reflect.set method in JavaScript sets the value of a property on an object, similar to target[propertyKey] = value but as a function. It belongs to the Reflect object and is one of the most widely used methods for working with reflect values in modern JavaScript and TypeScript applications.
The method signature is Reflect.set(target, propertyKey, value, receiver?). It accepts 4 parameters: target, propertyKey, value. When called, it returns a boolean indicating whether setting was successful. Understanding when and how to use set() helps you write more expressive, readable code.
Common use cases for Reflect.set include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like reflect-get, reflect-has, reflect-defineproperty, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for Reflect.set is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.
Browser Compatibility
Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.
Related Methods
Reflect.getGets the value of a property on an object, similar to target[propertyKey] but as a function
Reflect.hasReturns a boolean indicating whether the target object has the specified property, equivalent to the in operator
Reflect.definePropertyDefines a new property directly on an object or modifies an existing property, returning a boolean indicating success
More Reflect Methods
Other methods in the Reflect object
Related Tools
More Reflect Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.