Syntax
new Proxy(target, handler)
Reflect.get(target, key)Examples
Basic Proxy with a get Trap
Intercepting property access to add custom behavior, like logging or defaults.
const user = { name: "Alice", age: 25 };
const loggedUser = new Proxy(user, {
get(target, prop) {
console.log(`Reading property: ${prop}`);
return Reflect.get(target, prop); // forwards to the default behavior
}
});
console.log(loggedUser.name);
// "Reading property: name"
// "Alice"Validation with a set Trap
Using a Proxy to enforce rules whenever a property is assigned.
const validatedUser = new Proxy({}, {
set(target, prop, value) {
if (prop === "age" && typeof value !== "number") {
throw new TypeError("age must be a number");
}
return Reflect.set(target, prop, value);
}
});
validatedUser.age = 30; // works fine
// validatedUser.age = "old"; // throws TypeErrorDefault Values with a has and get Trap
Providing a fallback value for any property that does not exist.
function withDefault(obj, defaultValue) {
return new Proxy(obj, {
get(target, prop) {
return prop in target ? target[prop] : defaultValue;
}
});
}
const settings = withDefault({ theme: "dark" }, "not set");
console.log(settings.theme); // "dark"
console.log(settings.fontSize); // "not set" - property does not exist, but no errorBest practices
- Use Reflect methods inside Proxy traps to forward default behavior, rather than manually reimplementing property access logic
- Reach for Proxy for cross-cutting concerns like validation, logging, or reactivity systems - not as a general-purpose object wrapper for everyday code
- Be aware that Proxy adds a small performance overhead on every intercepted operation - avoid it in performance-critical hot paths
- Document any Proxy-based behavior clearly, since intercepted objects can behave in ways that are surprising to someone reading the code without that context
At a glance
- Purpose
- Application logic and interaction
- File extension
- .js ยท .mjs
- Runs in
- Browsers and JavaScript runtimes
- Usually used with
- HTML, CSS and Web APIs
Specifications & further reading
Related JavaScript documentation
Arrow Functions
Arrow functions provide a more concise syntax for writing function expressions in JavaScript. Introduced in ES6, they use the => syntax and have some important differences from regular functions, particularly in how they handle the "this" keyword. Arrow functions are especially useful for callbacks, array methods, and short inline functions.Variables: var, let, const
JavaScript has three ways to declare variables. var is the original way, function-scoped and hoisted with a default value of undefined. let, introduced in ES6, is block-scoped and can be reassigned. const is also block-scoped but cannot be reassigned after its initial value is set. Modern JavaScript strongly favors let and const over var.Data Types
JavaScript has seven primitive data types - string, number, boolean, undefined, null, bigint, and symbol - plus the object type, which includes arrays, functions, and plain objects. JavaScript is dynamically typed, meaning a variable can hold any type and that type can change. The typeof operator reports a value's type at runtime.Arithmetic Operators
Arithmetic operators perform mathematical calculations on numbers: addition (+), subtraction (-), multiplication (*), division (/), remainder/modulo (%), and exponentiation (**). The + operator also performs string concatenation when either operand is a string. Increment (++) and decrement (--) adjust a variable by one.
Arrow functions provide a more concise syntax for writing function expressions in JavaScript. Introduced in ES6, they use the => syntax and have some important differences from regular functions, particularly in how they handle the "this" keyword. Arrow functions are especially useful for callbacks, array methods, and short inline functions.Variables: var, let, const
JavaScript has three ways to declare variables. var is the original way, function-scoped and hoisted with a default value of undefined. let, introduced in ES6, is block-scoped and can be reassigned. const is also block-scoped but cannot be reassigned after its initial value is set. Modern JavaScript strongly favors let and const over var.Data Types
JavaScript has seven primitive data types - string, number, boolean, undefined, null, bigint, and symbol - plus the object type, which includes arrays, functions, and plain objects. JavaScript is dynamically typed, meaning a variable can hold any type and that type can change. The typeof operator reports a value's type at runtime.Arithmetic Operators
Arithmetic operators perform mathematical calculations on numbers: addition (+), subtraction (-), multiplication (*), division (/), remainder/modulo (%), and exponentiation (**). The + operator also performs string concatenation when either operand is a string. Increment (++) and decrement (--) adjust a variable by one.