Middle
What is the difference between arrow functions and Function Declaration?
sobes.tech AI
Answer from AI
Main differences between arrow functions and Function Declaration:
-
Syntax: Arrow functions are shorter and do not have their own name.
-
thiscontext: Arrow functions do not have their ownthis; they inherit it from the surrounding context. Regular functions have their ownthis, which depends on how they are called. -
argumentsobject: Arrow functions do not have anargumentsobject, whereas regular functions do. -
Cannot be used as constructors: Arrow functions cannot be called with
new. -
Lack of
prototype: Arrow functions do not have aprototypeproperty.
Example:
const obj = {
value: 42,
regularFunc: function() { return this.value; },
arrowFunc: () => this.value
};
console.log(obj.regularFunc()); // 42
console.log(obj.arrowFunc()); // undefined, because `this` is taken from the outer context