What is a Proxy in JavaScript?

What is a Proxy in JavaScript? Target object, Proxy and handler traps example

Post Date: March 30, 2020
Last Updated:
November 14, 2026

Introduction

JavaScript Proxy is a powerful feature that allows developers to customize the behaviour of objects. It acts as an intermediary between an object and its operations, allowing you to intercept and control actions such as reading properties, modifying values, checking properties, and calling functions.

A Proxy object wraps an existing object and uses a handler to define custom behaviour for different operations. These custom behaviours are implemented using special methods called traps.

JavaScript Proxies are useful for data validation, access control, logging, property manipulation, and creating reactive applications.

In this article, we will explore what a JavaScript Proxy is, its syntax, how it works, and practical examples to understand its usage.

What is a Proxy in JavaScript?

A Proxy in JavaScript is an object that allows you to define custom behaviour for fundamental operations performed on another object.

It consists of two main components:

  • Target: The original object that the Proxy wraps.
  • Handler: An object containing methods (called traps) that define custom behaviour for operations performed on the target.

Whenever an operation is performed on the Proxy, the corresponding handler method can intercept that operation and execute custom logic.

Basic Example of JavaScript Proxy

Let’s understand how a JavaScript Proxy works with a simple example.

Proxy in JavaScript example demonstrating the get handler and default property value 37

Explanation

In the above example:

  1. We create a handler object with a get method.
  2. The get method checks whether the requested property exists in the target object.
  3. If the property exists, it returns its corresponding value.
  4. If the property does not exist, it returns the default value 37.
  5. We create a Proxy object using the Proxy constructor.
  6. We assign values to properties a and b and access them through the Proxy.

Output:

JavaScript Proxy console output showing the values 1 undefined and false 37 from the get handler example.

Here, p.a returns 1 because the property exists. Similarly, p.b returns undefined because the property exists but its value is undefined.

However, p.c returns 37 because the property does not exist in the target object.

The in operator returns false because the handler does not define a has trap to customize property existence checks.

JavaScript Proxy Syntax

The basic syntax for creating a Proxy object is:

JavaScript Proxy syntax showing how to create a Proxy object using the target and handler parameters.

The constructor accepts two arguments:

  • target: The original object that you want to wrap with a Proxy.
  • handler: An object containing traps that define custom behaviour for operations performed on the target.

Common JavaScript Proxy Handler Methods

JavaScript provides several handler methods that allow developers to intercept different object operations.

Some commonly used Proxy handler methods are:

1. get()

The get() method intercepts property access operations.

Output:

John
Property not found

2. set()

The set() method intercepts operations that assign values to object properties.

Output:

Here, the Proxy validates the assigned value before updating the target object.

3. has()

The has() method intercepts the in operator when checking whether a property exists in an object.

Output:

4. apply()

The apply() method intercepts function calls when the target is a callable function.

Output:

The apply() trap executes custom logic before calling the original function.

What is Proxy.revocable() in JavaScript?

JavaScript also provides the Proxy.revocable() method, which creates a revocable Proxy object.

A revocable Proxy can be disabled whenever required by calling its revoke() method.

Syntax:

Example:

After calling revoke(), accessing the Proxy object throws a TypeError because the Proxy has been revoked.

Advantages of JavaScript Proxy

JavaScript Proxy provides several advantages:

  • Data Validation: Validate values before assigning them to object properties.
  • Access Control: Control access to specific object properties.
  • Property Interception: Intercept property access and modification operations.
  • Logging: Track object operations for debugging purposes.
  • Custom Behaviour: Define custom logic for different object operations.

Conclusion

JavaScript Proxy is a useful feature that allows developers to intercept and customize operations performed on objects and functions. By using a target object and a handler, developers can control property access, assignment, function calls, and other fundamental operations.

Proxy handler methods such as get(), set(), has(), and apply() make it easier to implement data validation, access control, logging, and custom object behaviour.

Understanding JavaScript Proxy helps developers write more flexible and maintainable JavaScript applications.

Frequently Asked Questions (FAQs)

1. What is a Proxy in JavaScript?

A Proxy in JavaScript is an object that wraps another object and allows developers to intercept and customize its fundamental operations using handler methods called traps.

2. What are the main components of a JavaScript Proxy?

A JavaScript Proxy has two main components: a target and a handler. The target is the original object, while the handler contains methods that define custom behaviour for operations performed on the target.

3. What is the use of Proxy.revocable() in JavaScript?

The Proxy.revocable() method creates a Proxy that can be disabled when required. It returns a Proxy object and a revoke() function. Calling the revoke() function disables the Proxy permanently.

How to use shift and unshift method in javascript

How to remove & install oracle java [Jdk] in ubuntu 16.0.4

Need Help with JavaScript Development?

Are you looking for professional assistance with JavaScript development, frontend optimization, web application development, or troubleshooting complex JavaScript issues?

Our development team at Pheonix Solutions can help you build, maintain, and optimize modern web applications using reliable development practices.

Get in touch with our team here to discuss your web development requirements.

admin

Our team has expertise across software and web development, WordPress, e-commerce, mobile applications, UI/UX, cloud and infrastructure, DevOps, CI/CD, API integration, security, testing, automation, and technical support. The team also works with AI-based software solutions, LLMs, AI workflows, AI agents, and intelligent application development to help businesses automate processes and build smarter digital solutions. We focus on developing, deploying, maintaining, and optimising secure, scalable, and reliable technology solutions while helping businesses adopt modern technologies and drive digital transformation.

Leave a Comment

Scroll to Top