Meta-Program with Reflect API
Write a JavaScript function that uses Reflect.get() to read a property from an object and Reflect.set() to write a property, returning the value before modification. The Reflect API provides methods for interceptable JavaScript operations that correspond to the Proxy traps.
The Reflect API, introduced in ES6, is a built-in object with static methods that mirror Proxy traps. Reflect.get(target, propertyKey) is equivalent to the bracket notation target[propertyKey] but returns undefined and can be used with Receiver for inheritance. Reflect.set(target, propertyKey, value) sets a property and returns a boolean indicating success, unlike the assignment operator which silently fails in some cases.
Reflect methods are the programmatic counterparts to internal language operations. They are useful for forwarding Proxy traps to the target object, implementing meta-programming patterns, and performing operations that would otherwise require try/catch or typeof checks. The Reflect API is unique to JavaScript among mainstream languages.
Time complexity is O(1) for both Reflect.get() and Reflect.set(). Space complexity is O(1). Reflect methods have the same performance characteristics as the corresponding language operations they mirror.
Edge cases include non-existent properties (Reflect.get returns undefined), read-only properties (Reflect.set returns false), and ensuring the correct receiver is used for inherited getters.
Example Input & Output
Old name is Alice
Old z is 42, set to -1
Old value of x is 10
Old val is 100
Old value of a is 1, set to 99
Algorithm Flow

Solution Approach
Best Answers
function solution(obj, key, val) {
var old = Reflect.get(obj, key);
Reflect.set(obj, key, val);
return old;
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
