Sobes.tech
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.

  • this context: Arrow functions do not have their own this; they inherit it from the surrounding context. Regular functions have their own this, which depends on how they are called.

  • arguments object: Arrow functions do not have an arguments object, whereas regular functions do.

  • Cannot be used as constructors: Arrow functions cannot be called with new.

  • Lack of prototype: Arrow functions do not have a prototype property.

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
What is the difference between arrow functions and… - sobes.tech