How to Natively Implement instanceOf in JavaScript

<p>The&nbsp;<code>instanceof</code>&nbsp;operator tests to see if the&nbsp;<code>prototype</code>&nbsp;property of a constructor appears anywhere in the prototype chain of an object. The return value is a boolean value.</p> <p>Interviewer: to write a fakeInstanceOf, you must meet the following test cases:</p> <pre> function fakeInstanceOf (instance, parent): Boolean {} //=&gt; true fakeInstanceOf([], Array) //=&gt; true fakeInstanceOf([], Object) //=&gt; true fakeInstanceOf(x =&gt; x, Object) //=&gt; false fakeInstanceOf(&#39;hello&#39;, Object)</pre> <p>The following is a reference</p> <pre> function fakeInstanceOf (instance, parent) { if (typeof(instance) !== &#39;object&#39; &amp;&amp; typeof(instance) !== &#39;function&#39;) { return false } let proto = instance?.__proto__ || null while (true) { if (proto === null) { return false } if (proto === parent.prototype) { return true } proto = proto.__proto__ } }</pre> <p><a href="https://javascript.plainenglish.io/how-to-natively-implement-instanceof-in-javascript-15d6eca383c2">Website</a></p>