JavaScript: Reflect.has

By Xah Lee. Date: . Last updated: .

New in JS2015.

Reflect.has(obj, key)

Return true if obj has property key key (string key or Symbol key), or in Prototype Chain. Else false.

Similar to key in obj [see “in” Operator] , but in a function form, so can be constructed at run-time.

const u = {p:3};
console.log( Reflect.has ( u , "p" ) ); // true
console.log( Reflect.has ( u , "y" ) ); // false

Example with property key in parent object.

// Reflect.has return true if property is own property or in prototype chain
const ob1 = {p:3};
const ob2 = Object.create (ob1);
console.log( Reflect.has ( ob2 , "p" ) ); // true

Example with Symbol key.

// Reflect.has works with symbol key

const sy = Symbol();

const ob = {};
ob[sy] = 4;   // add a symbol key

console.log( Reflect.has ( ob , sy ) ); // true

[see Access Property]

JavaScript Check Property Existence

BUY
ΣJS
JavaScript in Depth

JS Obj Reference

Reflect