how arrow functions work javascript code example
Example 1: arrow function javascript
// Normal Function in JavaScript
function Welcome(){
console.log("Normal function");
}
// Arrow Function
const Welcome = () => {
console.log("Normal function");
}
Example 2: js arrow function this
const person = {
firstName: 'Viggo',
lastName: 'Mortensen',
fullName: function () {
return `${this.firstName} ${this.lastName}`
},
shoutName: function () {
setTimeout(() => {
//keyword 'this' in arrow functions refers to the value of 'this' when the function is created
console.log(this);
console.log(this.fullName())
}, 3000)
}
}
Example 3: arrow function map js
const exampleArray = ['aa','bbc','ccdd'];
console.log(exampleArray.map(a => a.length));
//Would print out [2,3,4]
Example 4: how to make javascript function consise
multiplyfunc = (a, b) => { return a * b; }