Question 15 / 31

Does a same-named local variable shadow this.message?

Topic: this bindingDifficulty: Foundation

A local variable and a property read through this live in entirely separate namespaces.

Solve it with the execution visualizerStep through every binding, function call, and console output below.

Predict the output

Predict the output. The method declares a local message — does return this.message read that local, or the object property?

const object = {
  message: 'Hello, World!',
  getMessage() {
    const message = 'Hello, Earth!';
    return this.message;
  }
};
console.log(object.getMessage());
Reveal the answer and explanation

Expected output

Hello, World!

Why this happens

getMessage declares a local const message, but that binding only affects bare references to the identifier message, never this.message.

this.message is a property access: it resolves this to the receiver object and then looks up message on that object, completely bypassing the local variable of the same name.

Since object.getMessage() is a method call, this is bound to object, so this.message reads the object's own message property and logs "Hello, World!".

What this question tests

  • this.x is a property lookup, not an identifier lookup
  • local variables never shadow property accesses
  • implicit binding: a method call binds this to the receiver

Runtime assumptions

This distinction holds in any JavaScript environment — property access and variable resolution are always separate mechanisms.

Specification references

JavaScript Code EditorDirectly edit or paste custom code
Step 0 of 0
StepSpaceRun
Press → (Step) or Space (Run)
Phase 1: Compilation(Hoisting & Memory Setup)
Phase 2: Execution(Line-by-line Evaluation)
Execution Step Breakdown

Press "→" / click "▶" to step line-by-line, or press "Space" / click "▶ Run" for auto-play.