Arrow Function in JavaScript
What is arrow function in JavaScript?
Arrow function is one of the features introduced in ES6 for writing better
javascript.
Arrow function is similar to a function expression, but it does not have a
name. It is written as a function expression with parenthesis.
The function expression is followed by an arrow ( =>). The arrow function
expression is followed by an expression, which is returned by the arrow
function.
let message = (name) => { return `Hello ${name}`; }
Arrow function in javascript is an alternative way to write a function with
shorter and cleaner syntax than a regular function.
An arrow function is also called the fat arrow function.
Syntax of arrow function
The syntax of the arrow function is similar to the syntax of a function
expression, except that it does not have a name.
() => { ... } // no parameter
param1 => { ... } // one parameter
(param1, param2) => { ... } // multiple parameters
The above syntax shows a function with no parameters, a function with
one parameter, and a function with multiple parameters.
Example of arrow function
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Arrow function - javascript</title>
</head>
<body>
<h2>Creating simple arrow function in javascript.</h2>
<script>
const add = (a, b) => { return a + b };
document.write(add(1, 2));
</script>
</body>
</html>