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
};
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
Top comments (2)
Your
addfunction example can be much shorter:Yeah!