Problem Statement
Compare arrow functions and regular functions in JavaScript.
Explanation
Arrow functions and regular functions behave differently in how they handle 'this'.
Arrow functions do not create their own 'this'; they use the one from the outer scope.
Regular functions have their own 'this' and can be used as constructors.
Arrow functions are shorter and great for small callbacks, but avoid them in object methods.
In interviews, remember this rule: Arrow equals lexical this. Regular equals dynamic this.
Use arrow for compact logic, and regular when context matters.
Code Solution
SolutionRead Only
function normal() { console.log(this); }
const arrow = () => console.log(this);
const obj = {
normal,
arrow
};
obj.normal(); // obj context
obj.arrow(); // global or undefined (lexical this)