In JavaScript, we often don't need to name our functions, especially when passing a function as an argument to another function. Instead, we can create inline functions. We don't need to name these functions because we don't reuse them anywhere else.
There're two ways to use arrow functions. Let's see the syntax.
Syntax
const ArrowFun = function() {
const myVar = 'Value'
return myVar
}
We can omit the function keyword. See the below syntax which results similar to above
const ArrowFun = () => {
const myVar = 'Value'
return myVar
}
Also, there's this other way to write down an arrow function in JavaScript. Like, when there's no function body, and only a return statement, arrow function syntax allows to omit the keyword return as well as the brackets surrounding the code. Have a look on to a below code snippet. π½
const myFunc = () => 'Value'
The above mentioned code will still return the strong value by default.
const magic = {} => new Date() //returns a Date
Moreover, just like any other function, you can pass Params and other operators to the arrow functions in JavaScript.
Top comments (1)
Yes Mursal π, I also use arrow functions a lot during my day to day work. It's really really helps in making the codebase clean and simple for any new developers β.