JS: Reflect.has

By Xah Lee. Date: . Last updated: .

New in ES2015.

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 JS: “in” Operator] , but as a function instead of a operator.

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 both string and symbol keys

const sy = Symbol();

const ob = {};
ob["st"] = 3; // add a string key
ob[sy] = 4;   // add a symbol key

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

[see JS: Symbol Tutorial]

[see JS: Access Property]

Reference

ECMAScript® 2016 Language Specification#sec-reflect.has

back to JS: Reflect

JS Object Property Topic

  1. JS: Property Overview
  2. JS: Property Key
  3. JS: Property Dot Notation / Bracket Notation
  4. JS: Create/Delete Property
  5. JS: Get Property, Set Property
  6. JS: Check Property Existence
  7. JS: Access Property
  8. JS: Property Attributes, writable, enumerable, configurable
  9. JS: Getter/Setter Properties
  10. JS: Property Descriptor
Liket it? Put $5 at patreon.

Or, Buy JavaScript in Depth

If you have a question, put $5 at patreon and message me.