Arrow Functions in JavaScript
Arrow Functions, also known as Fat Arrows, are one of the simpler new features in ES6.
### Arrow Function without Parameter
const printFive = () => 5;
const result = printFive();console.log(result);
// 5
### Arrow Function with one Parameter
const doubleIt = number => number * 2;
const result = doubleIt(12);console.log(result);
// 24
### Arrow Function with Multiple Parameter
const sum = (num1, num2) => num1 + num2;
const result = sum(12,13);console.log(result);
// 25
### Arrow Function with Multiple Parameter and line
const findBig = (num1, num2) => {
if (num1 > num2) {
return num1;
}
else {
return num2;
}
}const result = findBig(3, 7);console.log(result);
// 7
###Functions with Default Parameter Values
Default parameters allow named parameters to be initialized with default values if no value or undefined
is passed.
const multiply = (a, b = 2)=> {
return a * b;
}console.log(multiply(5, 2));
// output: 10
### Functions Arguments
arguments
is an Array
-like object accessible inside functions that contains the values of the arguments passed to that function
function func1(a, b, c) {
let total = 0;
for(let i = 0; i < arguments.length; i++){
total = total + arguments[i];
}
return total;
}let results = func1(1, 2, 3, 4);
console.log(results);
# ## There are two major benefits of using Arrow functions. One is that it’s a shorter syntax and thus requires less code. The main benefit is that it removes the several pain points associated with this operator.