Reflect

Reflect.set

Sets the value of a property on an object, similar to target[propertyKey] = value but as a function

Syntax

JavaScript
Reflect.set(target, propertyKey, value, receiver?)

Parameters

ParameterTypeDescription
targetobjectThe target object to set the property on
propertyKeystring | symbolThe property name
valueanyThe value to set
receiveranyThe value of this for setters

Return Value

A boolean indicating whether setting was successful

Examples

Basic Usage
const obj: Record<string, number> = {}
Reflect.set(obj, 'x', 42)
console.log(obj.x) // 42
Practical Example
const handler: ProxyHandler<Record<string, unknown>> = {
  set(target, prop, value, receiver) {
    console.log(`Setting ${String(prop)} = ${value}`)
    return Reflect.set(target, prop, value, receiver)
  }
}
Advanced Usage
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

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.