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

  1. Property Overview
  2. Property Key
  3. Dot vs Bracket Notation
  4. Create/Delete Property
  5. Get/Set Property
  6. Check Property Existence
  7. Access Property
  8. List Properties
  9. Property Attributes
  10. Getter/Setter
  11. Property Descriptor
  12. Symbol
Liket it? Put $5 at patreon.

Or, Buy JavaScript in Depth

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

Web Dev Tutorials

  1. HTML
  2. Visual CSS
  3. JS Basics
  4. JS in Depth
  5. JS Reference
  6. DOM
  7. SVG
  8. JS Misc