The Reflect API is a built-in JavaScript object (introduced in ES6) that provides utility functions to perform common object operations in a safe, consistent, and predictable way.
It is similar to the Object methods but more powerful, more standardized, and used heavily with Proxies.
✅ Why was Reflect introduced?
To solve problems with older methods like:
- inconsistent behaviors
- throwing silent errors
- complex property manipulation
- lack of direct functions for Proxy traps
Reflect gives a unified API for handling objects.
✅ Reflect Provides Functions for:
✔ Object manipulation
Reflect.get()
Reflect.set()
Reflect.deleteProperty()
Reflect.has()
Reflect.defineProperty()
✔ Function calling
Reflect.apply()
Construct() // for new
✔ Working with prototypes
Reflect.getPrototypeOf()
Reflect.setPrototypeOf()
✔ Safe property definition (returns boolean instead of throwing)
Reflect.defineProperty(obj, "x", { value: 10 });
🔥 Example 1: Using Reflect.get() and Reflect.set()
const user = { name: "Teekam", age: 26 };
console.log(Reflect.get(user, "name")); // "Teekam"
Reflect.set(user, "age", 30);
console.log(user.age); // 30
🔥 Example 2: Reflect.deleteProperty()
const obj = { a: 1, b: 2 };
Reflect.deleteProperty(obj, "b");
console.log(obj); // { a: 1 }
🔥 Example 3: Reflect.apply() (call a function safely)
function sum(a, b) {
return a + b;
}
console.log(Reflect.apply(sum, null, [5, 7])); // 12
🔥 Example 4: Reflect with Proxy (Most Common Use Case)
Reflect ensures Proxy traps behave like normal operations.
const user = { name: "Teekam" };
const proxy = new Proxy(user, {
get(target, prop) {
console.log("Getting:", prop);
return Reflect.get(target, prop);
}
});
console.log(proxy.name);
✔ Without Reflect, you'd manually manage object access.
✔ Reflect prevents unexpected side effects.
🎯 Why Use Reflect Instead of Object Methods?
| Operation | Old way | Reflect way | Difference |
|---|---|---|---|
| Check property | "x" in obj |
Reflect.has(obj, "x") |
method form |
| Set property | obj.x = 10 |
Reflect.set(obj, "x", 10) |
returns true/false |
| Delete property | delete obj.x |
Reflect.deleteProperty(obj, "x") |
safer |
| Call function | func.apply() |
Reflect.apply() |
consistent |
Reflect methods:
- Fail safely (return boolean instead of throwing)
- More consistent
- Work perfectly with Proxy
- Better for meta-programming
🎯 Short Interview Answer
The Reflect API is an ES6 built-in object that provides methods to manipulate objects (get, set, delete, apply, defineProperty) in a more consistent and safe way. It is commonly used with Proxies and helps standardize low-level operations that previously used
Objectmethods or operators.