JS Arrow Function

Mursal Furqan Kumbhar - Jun 5 '22 - - Dev Community

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
}
Enter fullscreen mode Exit fullscreen mode

We can omit the function keyword. See the below syntax which results similar to above

const ArrowFun = () => {
     const myVar = 'Value'
     return myVar
}
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode

The above mentioned code will still return the strong value by default.

const magic = {} => new Date() //returns a Date
Enter fullscreen mode Exit fullscreen mode

Moreover, just like any other function, you can pass Params and other operators to the arrow functions in JavaScript.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player