Junior — Middle
What are the differences between arrow functions and regular functions in JavaScript?
sobes.tech AI
Answer from AI
Arrow functions in JavaScript differ from regular functions in several key aspects:
thiscontext: In arrow functions,thisis lexically bound to the context where the function was declared. In regular functions,thisis determined at call time and can change.- No own
arguments: Arrow functions do not have their ownargumentsobject. To access arguments, use parameters or rest operator. - Cannot be used as constructors: Arrow functions cannot be called with
newand do not have a [[Construct]] internal method. - No
prototype: Arrow functions do not have aprototypeproperty.
Example:
const obj = {
value: 42,
regularFunc: function() {
console.log(this.value); // 42
},
arrowFunc: () => {
console.log(this.value); // undefined, because `this` is from outer scope
}
};
obj.regularFunc();
obj.arrowFunc();