Skip to main content

What does Reflect do?

What Reflect is

Reflect is a built-in object (like Math or JSON) that contains static methods for working with objects and functions at a low level.

Put simply:

Reflect is a "unified API" for basic operations on objects: reading, writing, calling, creating, and so on. Its methods correspond to the same operations that can be intercepted through Proxy.

Why it is needed

Before ES6, operations like delete obj.prop or obj[prop] = value were performed via "built-in syntax", but had no functional equivalent. Reflect was introduced to:

  1. Make these operations available as functions;
  2. Simplify writing Proxy traps (execution can be delegated through Reflect);
  3. Make the API symmetric and safe.

Example 1 - safely reading and writing properties

javascript
const user = { name: "Tim" }; console.log(Reflect.get(user, "name")); // "Tim" Reflect.set(user, "age", 30); console.log(user.age); // 30

The equivalent:

javascript
user.name; // get user.age = 30; // set

But with Reflect you can use this functionally, passing target, key, receiver by hand.

Example 2 - removing a property

javascript
const user = { name: "Tim" }; delete user.name; // traditional syntax // The Reflect equivalent: Reflect.deleteProperty(user, "name");

The difference: Reflect.deleteProperty returns true/false instead of throwing an exception if the property is "configurable: false".

Example 3 - defining a property with flags

javascript
const user = {}; Reflect.defineProperty(user, "role", { value: "admin", writable: false }); console.log(user.role); // admin

(this is the functional equivalent of Object.defineProperty)

Example 4 - checking whether a property exists

javascript
const user = { name: "Tim" }; console.log("name" in user); // true console.log(Reflect.has(user, "name")); // true

Example 5 - getting the keys

javascript
const user = { a: 1, b: 2 }; console.log(Reflect.ownKeys(user)); // ['a', 'b']

Unlike Object.keys(), it returns all keys, including Symbol.

Example 6 - controlling the prototype

javascript
const user = {}; const proto = { greet() { console.log("hi"); } }; Reflect.setPrototypeOf(user, proto); Reflect.getPrototypeOf(user).greet(); // "hi"

Example 7 - calling a function

javascript
function sum(a, b) { return a + b; } console.log(Reflect.apply(sum, null, [2, 3])); // 5

This is the safe equivalent of Function.prototype.apply.call(sum, ...)

Example 8 - creating instances

javascript
class User { constructor(name) { this.name = name; } } const user = Reflect.construct(User, ["Tim"]); console.log(user.name); // "Tim"

The full list of Reflect methods

MethodWhat it does
Reflect.apply(target, thisArg, args)Calls a function with the given this and arguments
Reflect.construct(target, args)The equivalent of new target(...args)
Reflect.defineProperty(target, key, descriptor)Creates/changes a property
Reflect.deleteProperty(target, key)Removes a property (the equivalent of delete)
Reflect.get(target, key, receiver?)Returns the property's value
Reflect.set(target, key, value, receiver?)Sets the property's value
Reflect.has(target, key)Checks whether the property exists (in)
Reflect.ownKeys(target)Returns all keys (including Symbol)
Reflect.getOwnPropertyDescriptor(target, key)Returns the property's descriptor
Reflect.preventExtensions(target)Prevents adding new properties
Reflect.isExtensible(target)Checks whether properties can be added
Reflect.getPrototypeOf(target)Gets the prototype
Reflect.setPrototypeOf(target, proto)Sets the prototype

Example 9 - Reflect + Proxy (the classic pairing)

javascript
const user = { name: "Tim", age: 25 }; const proxy = new Proxy(user, { get(target, prop, receiver) { console.log(`Reading property: ${prop}`); return Reflect.get(target, prop, receiver); // delegate safely }, set(target, prop, value, receiver) { console.log(`Changing property: ${prop}`); return Reflect.set(target, prop, value, receiver); } }); proxy.name; // Reading property: name proxy.age = 30; // Changing property: age

Using Reflect inside traps guarantees the behavior is correct even if the object has getters, setters, or a prototype.

The difference between Reflect and Object

FeatureObjectReflect
OrientationMethods for working with an object's structureUniversal low-level operations
Returning errorsMethods can throw exceptionsMethods more often return false
FunctionalityOperators cannot be called as functionsThey can (for example, Reflect.deleteProperty instead of delete)
Connection to ProxyNoneMethods correspond 1-to-1 with Proxy traps

Summary

What it doesExample
Reads a propertyReflect.get(obj, "x")
Writes a propertyReflect.set(obj, "x", 10)
Checks for existenceReflect.has(obj, "x")
Removes a propertyReflect.deleteProperty(obj, "x")
Gets the keysReflect.ownKeys(obj)
Calls a functionReflect.apply(fn, thisArg, args)
Creates an objectReflect.construct(Class, args)
Works with the prototypeReflect.getPrototypeOf(obj) / Reflect.setPrototypeOf(obj, proto)

The main idea:

Reflect is a "mirror" of JavaScript's basic operations, in the form of functions. It makes code cleaner, safer, and more predictable, especially when working with Proxy.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.