Bird
Raised Fist0
Javascriptprogramming~10 mins

this in functions in Javascript - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - this in functions
Function called
↓
Determine 'this' context
↓
Called as method
↓
Called as function
↓
Called with call/apply/bind
↓
Arrow function
↓
Execute function body using 'this'
When a function runs, JavaScript decides what 'this' means based on how the function is called.
Execution Sample
Javascript
const obj = {
  name: 'Alice',
  greet() { console.log(this.name); }
};
obj.greet();
Calls greet as a method of obj, so 'this' refers to obj and prints 'Alice'.
Execution Table
StepCode Line'this' ValueActionOutput
1const obj = {...}N/ACreate object with name and greet method
2obj.greet()objCall greet as method, 'this' is obj
3console.log(this.name)objAccess this.name, which is 'Alice'Alice
4Function endsN/ANo more code to run
💡 Function finishes after printing 'Alice'
Variable Tracker
VariableStartAfter Call
thisundefined (before call)obj (inside greet)
Key Moments - 3 Insights
Why does 'this' refer to obj inside greet()?
'this' refers to the object before the dot when a method is called, as shown in execution_table step 2.
What if greet() was called without obj, like const f = obj.greet; f();?
Then 'this' would be undefined (in strict mode) or the global object, because the function is called standalone, not as a method.
How do arrow functions affect 'this'?
Arrow functions do not have their own 'this'; they use 'this' from the surrounding code, unlike normal functions.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'this' at step 2?
AThe global object
BThe obj object
Cundefined
Dnull
💡 Hint
Check the 'this' Value column in execution_table row for step 2.
At which step does the function print 'Alice'?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the Output column in execution_table to find when 'Alice' is printed.
If greet() was called as a standalone function, what would 'this' be inside it?
AThe greet function itself
Bobj
Cundefined in strict mode
DAlways the global object
💡 Hint
Refer to key_moments explanation about calling greet() without obj.
Concept Snapshot
this in functions (JavaScript):
- 'this' depends on how function is called
- Method call: 'this' = object before dot
- Standalone call: 'this' = undefined (strict) or global
- call/apply/bind can set 'this' explicitly
- Arrow functions inherit 'this' from surrounding scope
Full Transcript
In JavaScript, the meaning of 'this' inside a function depends on how the function is called. When a function is called as a method of an object, 'this' refers to that object. For example, calling obj.greet() sets 'this' to obj inside greet. If the function is called standalone, like just greet(), then 'this' is undefined in strict mode or the global object otherwise. Arrow functions do not have their own 'this'; they use 'this' from the surrounding code. This behavior is shown step-by-step in the execution table where obj.greet() calls greet with 'this' as obj, and the function prints the name 'Alice'.

Practice

(1/5)
1. What does this refer to inside a regular function called as obj.method()?
easy
A. The function itself
B. The global object (like window)
C. Undefined
D. The object obj that calls the method

Solution

  1. Step 1: Understand how this works in method calls

    When a function is called as a method of an object, this points to that object.
  2. Step 2: Apply to the example obj.method()

    Since method is called by obj, this inside method refers to obj.
  3. Final Answer:

    The object obj that calls the method -> Option D
  4. Quick Check:

    obj.method() means this = obj [OK]
Hint: Remember: this is the caller object in method calls [OK]
Common Mistakes:
  • Thinking this is always global
  • Confusing this with the function itself
  • Assuming this is undefined in method calls
2. Which of the following is the correct way to define a method inside an object so that this refers to the object?
easy
A. const obj = { method: () => { console.log(this); } };
B. const obj = { method() => { console.log(this); } };
C. const obj = { method() { console.log(this); } };
D. const obj = { method: function() => { console.log(this); } };

Solution

  1. Step 1: Understand function types and this

    Arrow functions do not have their own this; they inherit from the outer scope. Regular functions have their own this.
  2. Step 2: Check each option

    A uses arrow function, so this is inherited from outer scope (global), not the object. B uses concise method syntax, which binds this to the object. C uses invalid syntax (cannot define concise method with arrow). D uses invalid syntax (function() => not valid).
  3. Final Answer:

    const obj = { method() { console.log(this); } }; -> Option C
  4. Quick Check:

    Use concise method syntax for object methods [OK]
Hint: Use concise method syntax for correct this in objects [OK]
Common Mistakes:
  • Using arrow functions for object methods expecting this as object
  • Attempting invalid concise arrow syntax like method() => {}
  • Using invalid syntax like function() => {} inside object
3. What will be the output of the following code?
const obj = {
name: 'Alice',
greet() {
function inner() {
console.log(this.name);
}
inner();
}
};
obj.greet();
medium
A. undefined
B. Error
C. Alice
D. null

Solution

  1. Step 1: Analyze this inside inner function

    The function inner is a regular function called without an object, so this inside it is undefined in strict mode (or global object in non-strict).
  2. Step 2: Check what this.name prints

    Since this is undefined, this.name is undefined, so console.log prints undefined.
  3. Final Answer:

    undefined -> Option A
  4. Quick Check:

    Inner function called alone: this = undefined [OK]
Hint: Inner functions called alone have this undefined [OK]
Common Mistakes:
  • Assuming this inside inner function is the object
  • Expecting 'Alice' to print
  • Confusing arrow functions with regular functions
4. Identify the error in the following code and fix it so that this.name inside inner refers to obj.
const obj = {
name: 'Bob',
greet() {
function inner() {
console.log(this.name);
}
inner();
}
};
obj.greet();
medium
A. Bind inner to this using inner.bind(this)()
B. All of the above
C. Assign const self = this and use self.name inside inner
D. Change inner to an arrow function

Solution

  1. Step 1: Understand why this is wrong inside inner

    Because inner is a regular function called alone, this is undefined, not obj.
  2. Step 2: Explore fixes to bind this correctly

    Change inner to an arrow function: Using arrow function for inner inherits this from greet, which is obj. Bind inner to this using inner.bind(this)(): Using inner.bind(this)() explicitly sets this to obj. Assign const self = this and use self.name inside inner: Saving this in self and using self.name works because self is accessible inside inner.
  3. Step 3: Conclusion

    All three methods correctly fix the problem.
  4. Final Answer:

    All of the above -> Option B
  5. Quick Check:

    Bind or arrow or self variable fixes this [OK]
Hint: Use arrow, bind, or self variable to fix this in inner functions [OK]
Common Mistakes:
  • Only trying one fix and missing others
  • Not understanding arrow functions inherit this
  • Forgetting to call bind method
5. You want to create an object counter with a method count that increases a number every second and logs it using this.value. Which code correctly keeps this referring to counter inside setInterval callback?
const counter = {
value: 0,
count() {
setInterval(function() {
this.value++;
console.log(this.value);
}, 1000);
}
};
counter.count();
hard
A. All of the above
B. Bind this to the callback: setInterval(function() { ... }.bind(this), 1000);
C. Save const self = this before setInterval and use self.value inside callback
D. Replace the callback with an arrow function: setInterval(() => { this.value++; console.log(this.value); }, 1000);

Solution

  1. Step 1: Identify the problem with this in setInterval

    The callback function is a regular function, so this inside it is not counter but undefined or global object.
  2. Step 2: Explore solutions to keep this as counter

    Replace the callback with an arrow function: setInterval(() => { this.value++; console.log(this.value); }, 1000);: Arrow functions inherit this from count, so this.value works. Bind this to the callback: setInterval(function() { ... }.bind(this), 1000);: Using bind(this) explicitly sets this inside callback. Save const self = this before setInterval and use self.value inside callback: Saving this in self and using self.value works because self is accessible inside callback.
  3. Step 3: Conclusion

    All three methods correctly keep this referring to counter.
  4. Final Answer:

    All of the above -> Option A
  5. Quick Check:

    Arrow, bind, or self variable all fix this in callbacks [OK]
Hint: Use arrow, bind, or self variable to keep this in callbacks [OK]
Common Mistakes:
  • Using regular function without binding this
  • Expecting this to be counter automatically
  • Not saving this before callback
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard