By Hemanta Sundaray on 2022-04-26
ES6 introduced arrow function syntax, a shorter way to write functions by using the special "fat arrow" () => notation.
Arrow functions remove the need to type out the keyword function every time you need to create a function. Instead, you first include the parameters inside the () and then add an arrow => that points to the function body surrounded in { } like this:
const rectangleArea = (width, height) => {
const area = width * height
return area
}
console.log(rectangleArea(6, 2))
// 12
JavaScript provides several ways to refactor arrow function syntax. The most condensed form of the function is known as concise body. Below, we’ll explore a few of these techniques:
So, if we have a function:
const squareNum = num => {
return num * num
}
We can refactor the function to:
const squareNum = num => num * num
Notice the following changes: