Arrow Functions
Arrow Functions
Arrow functions are a shorter way to write functions.
Traditional:
function greet(){
console.log("Hello");
}
Arrow:
const greet = () => {
console.log("Hello");
};
Output:
Hello
Syntax
const functionName = () => {
// code
};
Single-Line Arrow Function
Traditional:
function add(a,b){
return a+b;
}
Arrow:
const add = (a,b) => a+b;
What Happens?
(a,b) => a+b
Automatically means:
(a,b) => {
return a+b;
}