Why do we need arrow functions?

Why do we need arrow functions?

What are Arrow functions?

  • Arrow functions introduced in ES6 provide a concise way to write functions in JavaScript.

  • Content under arrow functions is lexically or statically defined.

  • It is also known as "fat arrow functions".

The function written is ES5 as:

function Add22andReturn(num) {
    let sum = 0;
    sum = num + 22;
    return sum;
}

The function written is ES6 as:

const Add22andReturn = num => num + 22
  • An arrow function expression is an anonymous function expression written with the “fat arrow” syntax (=>).

  • Arrow functions are mostly used in react too.

Arrow Function Behavior

  • Arrow functions have some functional behavior that do not have their own this binding or prototype and cannot be used as a constructor.

  • They grant the ability to omit parentheses around parameters and add the concept of a concise function body with the implicit return.

const sum = (a, b) => a + b

Conclusion

  • An arrow function doesn’t have its binding to this or super.

  • Arrow function declare as (args) => expression;

  • For multiple statements: (args) => { statements }.

  • An arrow function doesn’t have arguments object, new.target keyword, and prototype property.

Thank you for reading! Hope you liked it and learned something from it.