What does Reflect do?
What Reflect is
Reflect is a built-in object that contains a set of helper methods for working with objects:
accessing properties, deleting them, creating them, calling functions, and so on.
In other words:
Reflect is a set of "safe" counterparts to JS operators (in, delete, [], Object.defineProperty(), etc.),
gathered in one place.
The main goals of Reflect:
- A single interface for low-level operations on objects
→ instead of different operators and functions (
delete,in,Object.defineProperty, etc.) - Working alongside Proxy → so you can delegate actions back to the original object.
- More predictable behavior
→ methods return
true/falseinstead of throwing errors when an operation is impossible.
Examples of popular Reflect methods
| Method | What it does | Counterpart without Reflect |
|---|---|---|
Reflect.get(obj, prop) | Returns the value of a property | obj[prop] |
Reflect.set(obj, prop, value) | Sets the value of a property | obj[prop] = value |
Reflect.has(obj, prop) | Checks whether a property exists | "prop" in obj |
Reflect.deleteProperty(obj, prop) | Deletes a property | delete obj[prop] |
Reflect.ownKeys(obj) | Returns all keys (including Symbol) | Object.getOwnPropertyNames(obj).concat(Object.getOwnPropertySymbols(obj)) |
Reflect.defineProperty(obj, prop, desc) | Defines a property | Object.defineProperty(obj, prop, desc) |
Reflect.getOwnPropertyDescriptor(obj, prop) | Gets a property descriptor | Object.getOwnPropertyDescriptor(obj, prop) |
Reflect.apply(fn, thisArg, argsArray) | Calls a function with a context and arguments | fn.apply(thisArg, argsArray) |
Reflect.construct(fn, argsArray) | Creates an instance (like new fn(...)) | new fn(...argsArray) |
A usage example
javascript
const user = { name: "Tim" };
console.log(Reflect.get(user, "name")); // "Tim"
Reflect.set(user, "age", 25);
console.log(user); // { name: "Tim", age: 25 }
console.log(Reflect.has(user, "age")); // true
Reflect.deleteProperty(user, "age");
console.log(Reflect.has(user, "age")); // falseReflect and Proxy together
Reflect is often used inside Proxy traps,
to "forward" the default behavior back to the target object (target).
Example:
javascript
const user = { name: "Tim" };
const proxy = new Proxy(user, {
get(target, prop, receiver) {
console.log(`Reading property: ${prop}`);
// safer than target[prop]:
return Reflect.get(target, prop, receiver);
},
set(target, prop, value, receiver) {
console.log(`Writing property: ${prop} = ${value}`);
return Reflect.set(target, prop, value, receiver);
}
});
proxy.name; // Log: Reading property: name
proxy.age = 30; // Log: Writing property: age = 30Why do this?
Because Reflect guarantees the correct forwarding of this and the internal context,
unlike direct access via target[prop].
Summary
| Item | Description |
|---|---|
Reflect | a built-in object with methods for working with objects |
| Goal | unify low-level operations (reading, writing, deleting, creating, etc.) |
| Feature | safe operator counterparts that return true/false instead of errors |
| Often used | inside Proxy traps |
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.