JavaScript language core
Closures, prototypes, this-binding, coercion, modules. The topics behind 80% of JS trivia rounds — and behind real bugs.
The mental model
JavaScript has exactly two lookup chains, and almost every "weird JavaScript" question is one of them being followed correctly while you expected the other.
- The scope chain answers *"where does this variable live?"* It is fixed when the function is written (lexical), and a closure is nothing more than a function that kept its scope chain alive after the outer call returned.
- The **prototype chain** answers *"where does this property live?"* It is followed when you read
obj.xandobjhas no ownx, hopping up through[[Prototype]]links until it finds one or hitsnull.
this belongs to neither. this is not a variable and is not looked up in either chain: for a normal function it is decided **at the call site**, by how the function was called. For an arrow function it is not decided at all — arrows have no this binding, so the identifier resolves lexically through the scope chain like any other variable. That one sentence removes most this confusion permanently.
The analogy: scope is the *building you work in* — offices nested inside floors inside a building, and you can always see out to the corridor but never into a colleague's office. Prototype is *the org chart* — if you cannot answer a question, you escalate to your manager, then their manager, until someone answers or you reach the CEO (Object.prototype) and then null. And this is the meeting room you happen to be standing in when someone calls your name: change the room, change the answer.
What breaks without this model is not trivia. A this that becomes undefined after a method is passed as a callback, a var in a loop capturing the wrong value, a config object whose defaults leak into every instance because they were shared on the prototype, and a == comparison that quietly converts an object to "[object Object]" are all production bugs, and all four are one of these three mechanisms doing exactly what it is specified to do.
- Deep dive
- Internal architecture
- Real-world use cases
- Best practices
- Common mistakes
- Performance
- Security
- Production troubleshooting
- Advantages & disadvantages
- Alternatives
- Interview expectations
- Interview questions
- Facts to remember
- Cheat sheet
- Quick revision