Let's now look at arrow functions, which simplify the function syntax.
In the following code example, an ordinary function is written first, and the corresponding arrow function is written second (both functions do the same thing):
let func1 = function(num1, num2) {
let result = num1 * num2;
return result;
}
let func2 = (num1, num2) => {
let result = num1 * num2;
return result;
}
If the function has one line of code,
then in arrow functions you can not
write return
and curly braces:
let func1 = function(num1, num2) {
return num1 * num2
}
let func2 = (num1, num2) => num1 * num2;
If there is only one parameter of the arrow function, parentheses can be omitted:
let func1 = function(num) {
return num * num;
}
let func2 = num => num * num
If the function has no parameters at all, you need to write empty parentheses:
let func1 = function() {
console.log('!!!');
}
let func2 = () => console.log('!!!')