has() - JavaScript Tutorial

has()

BLUF: This tutorial on has() provides an in-depth look at JavaScript's core features. It includes practical examples and code snippets to help you master modern JS development.
Key Discovery: has()

Understanding has() is crucial for building dynamic, interactive web applications. Explore the examples below to see it in action.

The static method Reflect.has serves the purpose of determining whether a specific property is present within an object. It functions similarly to the in operator but operates as a callable function.

Syntax:

Example

Reflect.has(target, propertyKey)

Parameters:

target: It refers to the entity in which the desired property is sought.

propertyKey: This refers to the identifier of the property that is subject to verification.

Return value:

It provides a Boolean value that signifies whether the target possesses the specified property.

Exceptions:

A TypeError, if the target is not an Object.

Browser Support:

Chrome 49
Edge 12
Firefox 42
Opera 36

Example 1

Example

const object1 = {
  property1: 42
};
console.log(Reflect.has(object1, 'property1'));

Output:

Example 2

Example

const object1 = {
  property1: 42
};
console.log(Reflect.has(object1, 'property2'));

Output:

Example 3

Example

var x = { foo: 1 };
console.log(Reflect.has(x, 'foo'));
console.log('foo' in x);
console.log(Reflect.has(x, 'bar'));
console.log('bar' in x);

Output:

Output

true
true
false

false

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience