Reflect API in JavaScript
Read, write, and delete properties in JavaScript with Reflect methods. Refused changes to a fixed property return false; invalid targets can still throw errors.
Run this example once in a fresh browser console. The console shows the printed results.
1const desk = { room: 'B12', seats: 4 };2console.log('Room:', Reflect.get(desk, 'room'));3console.log('Write seats:', Reflect.set(desk, 'seats', 6));4console.log('Has seats:', Reflect.has(desk, 'seats'));5Object.defineProperty(desk, 'code', { value: 'LAB', writable: false, configurable: false });6console.log('Write fixed code:', Reflect.set(desk, 'code', 'NEW'));7console.log('Delete fixed code:', Reflect.deleteProperty(desk, 'code'));8console.log('Code:', desk.code);9console.log('Seats:', desk.seats);
Sample Output
Write seats: true
The ordinary property write succeeded and stored six.
Write fixed code: false; Delete fixed code: false
The descriptor prevents changing or deleting code. Reflect reports refusal instead of bypassing it.
In this one
- Read and write through Reflect
- Inspect operation success booleans
- Observe non-writable properties
- Forward object operations predictably
How it works
- 1
Read the desk room with Reflect.get.
- 2
Set seats to six and inspect the true result.
- 3
Create a non-writable, non-configurable code property.
- 4
Try to change and delete code through Reflect.
- 5
Read false for both refused actions and confirm LAB and six remain stored.
Edge cases
Non-writable property
A write can be refused without changing the value.
Check the boolean from Reflect.set. In this case code remains LAB.
Wrong target type
Reflect methods require suitable object targets.
A primitive target can throw a TypeError. Returning booleans for normal refusal is not universal error handling.
What's going on
Reflect is a built-in object with methods for ordinary object operations, such as reading, writing, and deleting properties. It makes those operations available as function calls. This lab-desk example shows both a successful update and an update the object refuses.
Reflect.get reads room, Reflect.set changes seats and returns true, and Reflect.has checks whether the seats property exists. Object.defineProperty then creates a fixed code property. writable false prevents changing its value, and configurable false prevents deleting it or changing certain property rules.
Reflect.set and Reflect.deleteProperty return false for those refused operations. They do not silently make the fixed property writable. This boolean result is useful inside proxy traps, which are handlers that customize object operations. Reflect is not a way around object restrictions, and it does not mean every possible mistake becomes false. Invalid arguments, such as using a non-object target for Reflect.get, can still throw errors.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What does Reflect.set return for a normal accepted write?
Pick an answer to check it.
Question 2
Can Reflect.set bypass writable false?
Pick an answer to check it.
Questions people ask
Is Reflect a constructor?
No. Use its methods directly, such as Reflect.get. Do not call new Reflect.
Why does deleting code return false?
The property was defined with configurable false, which prevents deletion.
Does Reflect.has check only own properties?
No. It also sees inherited properties, much like the in operator. Use Object.hasOwn for only direct properties.