use of anonymous function in javascript code example
Example 1: js anonymous function es6
// (param1, param2, paramN) => expression
// ES5
var multiplyES5 = function(x, y) {
return x * y;
};
// ES6
const multiplyES6 = (x, y) => { return x * y };
Example 2: anonymous function javascript
// Arrow functions
// ES6 introduced arrow function expression that provides a shorthand for declaring anonymous functions:
// For example, this function:
let show = function () {
console.log('Anonymous function');
};
// Code language: JavaScript (javascript)
// … can be shortened using the following arrow function:
let show = () => console.log('Anonymous function');
let show = (variable) => { /* code */ }
// Code language: JavaScript (javascript)
// Similarly, the following anonymous function:
let add = function (a, b) {
return a + b;
};
// Code language: JavaScript (javascript)
// … is equivalent to the following arrow function:
let add = (a, b) => a + b;