Wednesday, September 25, 2024
Google search engine
HomeLanguagesJavascriptJavaScript Symbol hasInstance Property

JavaScript Symbol hasInstance Property

JavaScript Symbol hasInstance is used to determine if a given constructor object recognizes the object as its instance.

Syntax:

[Symbol.hasInstance](Object)  

Parameters: It accepts a parameter “object”.

Return value: This returns true if the value is in the chain of the object otherwise false.
JavaScript code to show the working of this function.

Below examples illustrate the JavaScript Symbol hasInstance Property:

Example 1:

javascript




// Initialising some objects
let obj1 = [1, 2, 3];
let obj2 = ['a', 'b', 'c'];
let obj3 = [123];
let obj4 = [];
 
// Calling Symbol.hasInstance Property
console.log(Array[Symbol.hasInstance](obj1));
console.log(Array[Symbol.hasInstance](obj2));
console.log(Array[Symbol.hasInstance](obj3));
console.log(Array[Symbol.hasInstance](obj4));


Output:

true
true
true
true

Example-2:

javascript




// Calling a user define function 
function gfg() { }
 
// Initialising the object
let Script = new gfg
 
// Calling the Symbol.hasInstance property
console.log(gfg[Symbol.hasInstance](Script));


Output:

true

Supported Browsers: 

  • Google Chrome 50 above
  • Firefox 50 above
  • Edge 15 above
  • Opera 37  above
  • Apple Safari 10 and above

We have a complete list of Javascript symbols’ properties and methods, to check those please go through the Javascript Symbol Complete Reference article.

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments