โ† JavaScript
#6 most askedVery commonMediumthis & prototypes

How does `this` work?

๐Ÿข Asked in real interviews
No reports yet. Were you asked this? Be the first to say where.
I was asked this

What decides the value of this in JavaScript? Predict the output.

๐Ÿ”ฎ Predict the output

class User {
  constructor() { this.name = "Ada"; }
  regular() { return this?.name; }
  arrowInside() { return [1].map(() => this.name)[0]; }
  plainInside() { return [1].map(function () { return this?.name; })[0]; }
}
const u = new User();
const loose = u.regular;
console.log(u.regular(), loose(), u.arrowInside(), u.plainInside());

๐ŸŽค Answer it out loud, then check yourself

Say your answer as if the interviewer is listening (30โ€“90 seconds). Then reveal what a strong answer covers and tick what you said.

    Answer out loud first, then compare.
    Want to rehearse it?
    Answer this to the AI interviewer and get a verdict โ†’