How Javascript arrow functions make life easy

How Javascript arrow functions make life easy

There is No web developer out there who has not come across arrow functions. They can be super confusing with how the syntax is written & the number of variations it is used in.

First, lets write a simple function to add 2 numbers.

So, the addition function takes 2 arguments and returns their sum. Before we do the sum, we have to ensure to convert the incoming argument to Number type.

function addition(num1, num2){
      return Number(num1) + Number(num2);
}

Now, the same function as an arrow function

const addition = (num1, num2) => Number(num1) + Number(num2);

And, that its. It becomes a one-line function.

We have an Arrow function declared as a variable. The curly braces are replaced by an arrow and no need to explicitly write return, as return is implied in an arrow function.

We can use the arrow function like this by simply calling the variable-function and feeding in the 2 parameters.

console.log(addition(5,5));