JavaScript this binding in arrow functions vs regular functions

🚨 JavaScript Interview Question What will be the output? const obj = { name: "Frontend", regularFunc: function () { console.log(this.name); }, arrowFunc: () => { console.log(this.name); } }; obj.regularFunc(); obj.arrowFunc(); Both functions are inside the same object. But the output is different. This question tests understanding of: • this binding • Arrow functions vs regular functions • Execution context in JavaScript Many developers assume both will print the same value. What do you think the output will be? #JavaScript #FrontendInterview #ReactJS #FrontendDeveloper #JavaScriptConcepts #ProductBasedCompany

The scope of this keyword inside object method is reference to the object whic calls the methodwhich means this = objand this.name = obj.name => 'Frontend';whereas arrow function donot have their own this . They inherit this from the surrounding scope. Thats why obj.arrowFunc() will give undefined

Frontend (because of the value of this inside a regular function depends on how the function is called.) undefined (Arrow functions do not have their own this.)

See more comments

To view or add a comment, sign in

Explore content categories