Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

324
Views
How does instanceof operator actually work in JS?

I've searched the site for this question and got the following answer:

function instance_of(V, F) {
  var O = F.prototype;
  V = V.__proto__;
  while (true) {
    if (V === null)
      return false;
    if (O === V)
      return true;
    V = V.__proto__;
  }
}

It's from https://stackoverflow.com/a/9220317/12974414.

But I tested the following code, found something weird:

console.log( (10).__proto__ === Number.prototype ) // true
console.log( instance_of(10, Number ) )            // true
console.log( 10 instanceof Number )                // false

function instance_of(V, F) {
  var O = F.prototype;
  V = V.__proto__;
  while (true) {
    if (V === null)
      return false;
    if (O === V)
      return true;
    V = V.__proto__;
  }
}

How to explain this and how does instanceof operator actually work in JS ?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

JavaScript has a feature called autoboxing which converts primitives to Objects when you try to access properties on them.

instanceof doesn't autobox and a primitive number is not an instance of the Number class.

You can see, in the specification:

  1. If Type(O) is not Object, return false.
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!