DEV Community

Jaisurya
Jaisurya

Posted on

Arrow function in JavaScript

In JavaScript, arrow functions provide a short and simple way to define functions using the => symbol, which offers implicit return values and lexical scoping that makes them a popular choice to write cleaner and more readable code.

In JavaScript, an arrow function can be written with the use of the => symbol, which makes than shorter and compact.

Arrow functions in JavaScript don't have their own this, and they need to inherit this from the surrounding context.

Arrow functions only have a single expression that returns implicit, which makes the code more concise.

Syntax:

The arrow function syntax provides a shorter and more concise way to write functions in JavaScript.

(const functionName = (parameters) => {  
    // code to be executed  
};  
Enter fullscreen mode Exit fullscreen mode

Parameter: The parameters the function take are similar to the regular function expressions.

=>: This the fat arrow, which is used to separate parameters from the function body.

Function body: The code that the function executes is also similar to the regular function expressions.

Example code:

const add = (a, b) => {  
    return a + b;  
};  

console.log(add(10, 20));  

//Output: 30
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited

Your add function example can be much shorter:

const add = (a, b) => a + b
Enter fullscreen mode Exit fullscreen mode
Collapse
 
jaisurya profile image
Jaisurya

Yeah!