Lambda function denoted by
=>
Lambda function is a shortened version of the unnamed function.
The lambda function is used in the same way as the unnamed function. The unnamed function is used as follows:
•
we pass the unnamed function as a parameter to another function... Example:
Calculate(15, 7, function(v1, v2) {return v1+v2;});
•
create a new variable and assign an unnamed function to the new variable... Example:
var myFunc1 = function (a, b) { return a + b; } ;
JavaScript
<html>
<body>
<script>
// declare an unnamed function with 2 parameters and returning a + b
var myFunc1 = (a, b) => a + b;
// call an unnamed function
var value = myFunc1(15, 7);
// value = 22
</script>
</body>
</html>
JavaScript
// create a new variable myFunc1
var myFunc1
JavaScript
// myFunc1 variable assign an unnamed function with 2 parameters and returning a + b
myFunc1 = (a, b) => a + b;
Html
<html>
<body>
<script>
// Function declaration Calculate
function Calculate(a, b, funcFormula) // parameter funcFormula there is a function because it is used as a function
{
return funcFormula(a, b);
}
</script>
<script>
// Call the function Calculate
var value = Calculate(15, 7, (value1, value2) => // it is a lambda function that counts summation
{
return value1 + value2;
}
);
// value = 22
// Call the function Calculate
var value = Calculate(23, 12, (value1, value2) => // it is a lambda function that counts subtraction
{
return value1 - value2;
}
);
// value = 11
</script>
</body>
</html>