skip to content
Dev Journal

Arrow Function

/ 1 min read

Arrow functions are a concise way to write functions in ES6, making code shorter and potentially reducing errors. They use a fat arrow like this => instead of the function keyword.

Basic Syntax

  • Normal Function:
const normalFunction = function (arg1, arg2) {
// Do something
}
  • Arrow Function:
const arrowFunction = (arg1, arg2) => {
// Do something
}

Syntax Variations

  • Zero Arguments:
const zeroArgs = () => {/* do something */}
const zeroWithUnderscore = _ => {/* do something */}
  • One Arguments:
const oneArg = arg1 => {/* do something */}
const oneArgWithParenthesis = (arg1) => {/* do something */}
  • Two or More Arguments:
const towOrMoreArgs = (arg1, arg2) => {/* do something */}

Implicit Return

Arrow functions can have an implicit return if they contain a single line of code without curly braces.

  • Normal Functions:
const sumNormal = function (num1, num2) {
return num1 + num2
}
  • Arrow Functions:
const sumArrow = (num1, num2) => num1 + num2

Summary

Arrow functions simplify syntax and offer features like implicit returns, making your code more readable and easier to write.