Understanding this in JavaScript — Made Simple with Real Examples! Most Asked Interview Questions

Understanding this in JavaScript — Made Simple with Real Examples! Most Asked Interview Questions

Try to guess the answers to the following most commonly asked questions in interviews.

📌 1. Strict Mode + Regular Function


📌 2. Object Method vs Arrow Function


📌 3. Arrow Function Inside Method


📌 4. setTimeout + Regular Function


📌 5. Destructured Method Loses Context


📌 6. Class + setTimeout


📌 7. Fixing this inside nested functions

Fix using arrow or bind:


📌 8. Arrow functions do NOT bind this


📌 9. map + this context

Fix with arrow:


📌 10. Arrow Function from Class Method


Conclusion:

Key Observations from Code Examples

  • Arrow functions do not bind their own this. They inherit from the lexical (outer) scope.

  • Regular functions inside setTimeout, map, or nested scopes lose this unless bound explicitly.

  • Destructuring methods from objects can detach them from their original context → leading to undefined this.

  • Use .bind(), call(), or arrow functions to retain proper context in callbacks or asynchronous functions.

To view or add a comment, sign in

Others also viewed

Explore topics