Arrow Function
Wooooooow. I like arrow function in JavaScript so much.
Example 1:
Normal Function:
function addition(a, b){
return a + b
}
If the above function is converted into arrow function, then the result will be:
const addition = (a, b) => a + b
Rule: firstly, function keyword is omitted as the function is assumed here. Then, the actual function is assigned to a variable followed by syntax (values - here : a + b). Then, a fat arrow is written (=>). The return is removed. Btw, the curly braces are removed. And that's it.
Ex2: Normal function:
function isNegative(number){
return number < 0
}
Arrow Function:
const isNegative = (number) => number < 0
Ex3: Normal Function:
function randomNumber(){
return Math.random
}
Arrow Function:
const randomNumber = () => Math.random
Ex4: Normal Function:
document.addEventListener("Click", function(){
console.log("Click")
})
Arrow Function:
document.addEventListener("Click", () => console.log("Click"))
In case of ex4, the function is not needed to assign to a variable.