There is another simplified way to declare a function, which is often better than Function Expression known as 'Arrow functions'
Function Declaration:
function sum(a, b) { return a + b; }; alert( sum(1, 2) ); // 3
Function Expression:
let sum = function(a, b) { return a + b; }; alert( sum(1, 2) ); // 3
Arrow Function:
let sum = (a, b) => a + b; alert( sum(1, 2) ); // 3
If we have only one argument, then parentheses around parameters can be omitted, to make it shorter.
let square = n => n * n; alert( square(5) ); // 25
If there are no arguments, parentheses are empty, but they must be present:
let hello = () => alert('Hello!'); hello();
Arrow functions can be used in the same way as Function Expressions.
let age = prompt("Enter your age?"); let checkAge = (age < 18) ? () => alert('Young') : () => alert('Adult'); checkAge();
Sometimes we need a more complex function, with multiple expressions and statements. In that case, we can enclose function statements in curly braces. The major difference is that curly braces requires a return keyword to return a value
let sum = (a, b) => a + b; // "return" is not required alert( sum(1, 2) ); // 3
let sum = (a, b) => { let result = a + b; return result; //if curly braces used, then 'return' is required }; alert( sum(1, 2) ); // 3
Ad: