JS: Reflect.has

By Xah Lee. Date: . Last updated: .

New in JS2015.

Reflect.has(obj, key)

Return true if obj has (own or inherited) property key key (string key or Symbol key). Else false.

If obj is not Object Type , it's TypeError.

This function is similar to the syntax “in” Operator, but in a function form. Function form lets you easily manipulate it at run-time.

const xx = { x1: 3 };
const yy = { y1: 3 };
Object.setPrototypeOf(yy, xx);
// yy's parent is now xx

// check own property
console.log(Reflect.has(xx, "x1"));

// check inherited property
console.log(Reflect.has(yy, "x1"));

Example with Symbol key.

// Reflect.has works with symbol key

const ss = Symbol();

const jj = {};

// add a symbol key
jj[ss] = 4;

console.log(Reflect.has(jj, ss));
// if argument is not a object, it's TypeError
console.log(Reflect.has(3, "x"));
// error: Uncaught TypeError: Reflect.has called on non-object

JavaScript, Check Property Existence

BUY ΣJS JavaScript in Depth