Skip to main content

How does Reflect differ from Object?

Reflect and Object really are similar (both work with object properties), but they are built for different purposes. The difference between them is in philosophy, behavior, and use.

Let's break it down step by step:


Short answer

CriterionObjectReflect
Main purposeWorking with objects (creation, prototypes, properties)Metaprogramming - "controlling the behavior" of objects
AppearedSince the start of JSIn ES6 (ES2015)
Behavior on errorThrows exceptionsReturns true / false
Used inregular codeProxy, safe calls, internal operations
Return typeVaries (object, value, etc.)Almost always boolean or a primitive
Status"old" API"modern, reflective" API

Example - defineProperty

Object:

javascript
try { Object.defineProperty({}, 'x', { value: 1, writable: false }); console.log('ok'); } catch { console.log('Error!'); }

If the operation fails (for example, the object is frozen), it throws an error.


Reflect:

javascript
const obj = Object.freeze({}); const result = Reflect.defineProperty(obj, 'x', { value: 1 }); console.log(result); // false (no error)

Reflect.defineProperty() simply returns false instead of interrupting the program.


Example - removing a property

Via delete

javascript
const user = { name: 'Tim' }; delete user.name; // true

Via Reflect (safer)

javascript
const user = { name: 'Tim' }; console.log(Reflect.deleteProperty(user, 'name')); // true console.log(Reflect.deleteProperty(Object.freeze({x: 4}), 'x')); // false

Reflect.deleteProperty() does the same thing, but does not throw errors and returns a boolean result.


Example - calling a function

javascript
function greet(msg) { console.log(msg, this.name); } const user = { name: 'Tim' }; Reflect.apply(greet, user, ['Hi']); // "Hi Tim"

Reflect.apply() is an analog of Function.prototype.apply(), but more explicit and convenient for Proxy traps and metaprogramming.


Example - creating an object

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

Reflect.construct() is a "reflective" version of new, useful when you need to dynamically create a class instance.


Example - checking a property

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

Reflect.has() is the same as the in operator, but as a function (convenient for dynamic calls).


Example - getting and setting a property

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

An analog of user.name = ... and user.name, but in a "functional" form (convenient to use in Proxy).


Error behavior

MethodObjectReflect
Object.defineProperty(frozen, 'x', {...})throws an errorreturns false
Object.getPrototypeOf(null)TypeErrorreturns false
Object.setPrototypeOf(sealed, ...)throws an errorreturns false

So Reflect is a "quiet" and predictable variant of Object methods.


Methods that Object does not have

Reflect methodWhat it doesAnalog
Reflect.apply()Calls a function with the given this and argumentsfunc.apply()
Reflect.construct()Analog of the new operator-
Reflect.get() / Reflect.set()Gets / sets a property valueobj[key]
Reflect.has()Checks for a property'key' in obj
Reflect.deleteProperty()Deletes a propertydelete obj.key
Reflect.ownKeys()Returns all keys (including Symbol)Object.getOwnPropertyNames() + Object.getOwnPropertySymbols()

Real-world example - Proxy

Reflect works perfectly together with Proxy to pass through the default standard behavior.

javascript
const user = { name: 'Tim' }; const proxy = new Proxy(user, { get(target, key) { console.log(`Reading ${key}`); return Reflect.get(target, key); // safely delegate } }); console.log(proxy.name); // Output: // Reading name // "Tim"

Without Reflect.get() you would have to manually write target[key], which can cause errors in more complex cases (accessors, prototypes, etc.).


Summary

CriterionObjectReflect
PurposeWorking with properties and prototypesMetaprogramming, "reflecting" behavior
AppearedSince the start of JSES6 (2015)
On errorThrows an exceptionReturns false
Return valueVariesPredictable
Proxy supportNoYes
Convenience in functional callsMediumHigh

Short Answer

Interview ready
Premium

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