Arrow function variation in javascript.
stalin s
Posted on October 6, 2022
I have already discussed the arrow function in JavaScript in one of blogs.Arrow function .
In this blog let's deep dive in arrow function variations.
There are two factors that affects arrow functions.
1) The number of arguments required.
2) Whether you'd like an implicit return.
Number of arguments
zero arguments. : we can substitute the parenthesis with an underscore (_).
const zeroArgs = () => {/* do something */}
const zeroWithUnderscore = _ => {/* do something */}
One arguments. : we can remove parenthesis.
const oneArg = arg1 => {/* do something */}
const oneArgWithParenthesis = (arg1) => {/* do something */}
Two or More. : we will be the normal arrow function syntax.
const twoOrMoreArgs = (arg1, arg2) => {/* do something */}
Return type.
The below example would clearly describes how arrow functions handles return type.
// 3 lines of code with a normal function
const sumNormal = function (num1, num2) {
return num1 + num2
}
// Can be replaced with one line of code with an arrow function
const sumArrow = (num1, num2) => num1 + num2
Happy learning.
💖 💪 🙅 🚩
stalin s
Posted on October 6, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
javascript Goodbye Exceptions! Mastering Error Handling in JavaScript with the Result Pattern
November 22, 2024