Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST

Basic JavaScript

JS Tutorial JS Syntax JS Variables JS Operators JS If Conditions JS Loops JS Strings JS Numbers JS Functions JS Objects JS Dates JS Arrays JS Typed Arrays JS Sets JS Maps JS Math JS RegExp JS Data Types JS Errors JS Events JS Programming JS References JS Versions

JS Advanced

JS Functions JS Objects JS Classes JS Iterations JS Asynchronous JS Modules JS Meta Programming JS HTML DOM JS Windows JS Web API JS AJAX JS JSON JS jQuery JS Graphics JS Examples JS Reference


JavaScript Reflect

The Reflect Object

The Reflect object provides methods for working with JavaScript objects at a low level.

You can use Reflect to get, set, delete, and check properties in a consistent way.

MethodDescription
Reflect.get()Get a property value
Reflect.set()Set a property value
Reflect.has()Check if a property exists
Reflect.deleteProperty()Delete a property
Reflect.defineProperty()Define a new property
Reflect.getOwnPropertyDescriptor()Get property descriptor
Reflect.apply()Call a function
Reflect.construct()Create an object (like new)
Reflect.ownKeys()Get all keys (including symbols)
Reflect.isExtensibles()Check if object can grow
Reflect.preventExtensions()Stop object from growing

Why JavaScript Reflect?

Before Reflect, some operations were done using operators like in and delete, some operations were done using methods like Object.defineProperty, and some operations were used using language mechanisms like [[Get]] and [[Set]]).

Reflect brings all these into one clean API.

Before Reflect:

  • operations were scattered (in, delete, Object.*)
  • some returned errors, some returned booleans
  • some operators were "magic" and hard to intercept

With Reflect:

  • unifies everything
  • has predictable behavior
  • works perfectly with Proxy

Reflect was added to JavaScript ES6.


Reflect.has()

Example

// Create an Object
const person = {name: "John", lastname: "Doe"};

let answer = Reflect.has(person, "name");
Try it Yourself »

Same as using the in operator:

let answer = "name" in person;
Try it Yourself »

Reflect.deleteProperty()

Example

// Create an Object
const person = {name: "John", lastname: "Doe"};

Reflect.deleteProperty(person, "name");
Try it Yourself »

Same as using the delete operator:

delete person.name;
Try it Yourself »

When to Use Reflect?

CaseUseWhy
Getting / setting valuesYesConsistent return values
Creating objects like newYesReflect.construct() works with Proxy
Calling a function with contextYesReflect.apply() is cleaner than func.apply()
Meta programmingYesDesigned for low-level tasks
Simple object workNoUse normal JS syntax

Reflect.get()

You can use Reflect.get() to read Object properties:

Example

// Create an Object
const user = {name: "Jan", age: 40};

let age = Reflect.get(user, "age");
Try it Yourself »

Same as:

let age = user.age;
Try it Yourself »

Reflect.set()

You can use Reflect.set() to write Object properties:

Example

// Create an Object
const user = {name: "Jan", age: 40};

Reflect.set(user, "age", 41);

let age = Reflect.get(user, "age");
Try it Yourself »

Same as:

user.age = 41;
Try it Yourself »

Reflect.apply()

Example

function greet(message) {
  return message + ", " + this.name;
}

const person = {name: "Jan"};

let msg = Reflect.apply(greet, person, ["Hello"]);
Try it Yourself »

Same as:

let msg = greet.apply(person, ["Hello"]);
Try it Yourself »

Reflect.construct()

Example

// Create a new Array
const colors = Reflect.construct(Array, ["red", "green", "blue"]);
Try it Yourself »

Same as the new keyword:

const colors = new Array(["red", "green", "blue"]));
Try it Yourself »

Reflect.defineProperty()

Example

// Create an Object
const user = {};

// Add a Property
Reflect.defineProperty(user, "id", {
  value: 123,
  writable: false
});
Try it Yourself »

Same as the Object.defineProperty:

// Add a Property
Object.defineProperty(user, "id", {
  value: 123,
  writable: false
});
Try it Yourself »

Reflect.ownKeys()

Gets all keys.

Example

const sym = Symbol("secret");
const obj = { a: 1, [sym]: 2 };

let keys = Reflect.ownKeys(obj);
Try it Yourself »

Almost the same as:

let keys = Object.keys(obj);
Try it Yourself »

Note

Reflect.ownKeys() also returns symbols.


Reflect with Proxy (Very Common)

Proxy lets you intercept operations on objects:

  • Read a property
  • Write a property
  • Delete a property
  • Check property existence

Example

// Create an Object
const user = { name: "Jan", age: 40 };

// Create a Proxy
const proxy = new Proxy(user, {
  get(target, prop) {
    // safe forwarding
    return Reflect.get(target, prop);
  },
  set(target, prop, value) {
    // safe forwarding
    return Reflect.set(target, prop, value);
  }
});
Try it Yourself »


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.