JS: Reflect.has

By Xah Lee. Date: . Last updated: .

(new in ECMAScript 2015)

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.

🟢 TIP: This function is similar to the syntax in (operator), but in a function form. Function form lets you manipulate expressions at run-time.

const xdad = { pp: 3 };
const xson = {};
Reflect.setPrototypeOf(xson, xdad);

// check inherited property
console.assert(Reflect.has(xson, "pp") === true);

// check own property
console.assert(Reflect.has(xdad, "pp") === true);

Edge cases

Example. with Symbol Key

// Reflect.has works with symbol key

const jj = {};

// add a symbol key
const xsym = Symbol();
jj[xsym] = 4;

console.assert(Reflect.has(jj, xsym));

Argument is not a object

// if argument is not a object, it's TypeError

try {
 Reflect.has(3, "x");
} catch (xerror) {
 console.log(xerror);
}

// TypeError: Reflect.has called on non-object

JavaScript. Access Properties