Concise Body Arrow Functions

The ES6 version of javascript has a method to refactor the arrow function. The most concise method which is known as concise body.

  1. Concise body functions which have zero to one arguments need to to be enclosed inside parentheses Example:

// ZERO PARAMETERS
const functionName = () => {};

// ONE PARAMETERS
const functionName = (parameter) => {};

// TWO or MORE PARAMETERS
const functionName = (parameter1, parameter2) => {};
  1. A function body which has just a single line of code block does not need any curly braces, whatever the line interperates will be returned automactically, the contents of the code block starts from the => and the return keyword can be removed which is referred as implicit return.

Example:

// Single Line Block
const sumNumbers = (sum) => numbers + numbers;

// Multi-line Block
const sumNumbers = (numbers) => {
  const sum = numbers + numbers;
  return sum;
};

Last updated