JS: Reflect.has

By Xah Lee. Date: . Last updated: .

(new in JS: 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.

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 aa = { pp: 3 };
const bb = {};

// make bb parent to be aa
Reflect.setPrototypeOf(bb, aa);

// check own property
console.log(Reflect.has(aa, "pp"));

// check inherited property
console.log(Reflect.has(bb, "pp"));

Example. with Symbol Key

// Reflect.has works with symbol key

const jj = {};

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

console.log(Reflect.has(jj, xx));
// 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. Access Properties