xxxxxxxxxx
let greet = x => console.log(x);
greet('Hello'); // Hello
xxxxxxxxxx
// Non Arrow (standard way)
let add = function(x,y) {
return x + y;
}
console.log(add(10,20)); // 30
// Arrow style
let add = (x,y) => x + y;
console.log(add(10,20)); // 30;
// You can still encapsulate
let add = (x, y) => { return x + y; };
xxxxxxxxxx
// JavaScript ES5 function
function getGreeting() {
return 'Welcome to JavaScript';
}
// JavaScript ES6 arrow function with body
const getGreeting = () => {
return 'Welcome to JavaScript';
}
// JavaScript ES6 arrow function without body and implicit return
const getGreeting = () =>
'Welcome to JavaScript';
xxxxxxxxxx
// Traditional Function Expression
var add = function(a,b){
return a + b;
}
// Arrow Function Expression
var arrowAdd = (a,b) => a + b;
xxxxxxxxxx
const power = (base, exponent) => {
let result = 1;
for (let count = 0; count < exponent; count++) {
result *= base;
}
return result;
};