# Arrow Functions
TIP
Arrow functions which are introduced in ES helps in writing the functions in JavaScript in a concise manner
const func = ()=> { //some statements }
(param1,param2)=>{//some statement}
- about
anonymous functions
doesn't
have aname
attached to it- used during
function callback
- would create a
new
instance ofthis
, hides outer scope ofthis
# with
Array.prototype.map()
names.map((element,index)=> {
console.log('index is'+ index + 'element value is :'+ element)
})
window.setTimeout()
setTimeout(()=>{
console.log('Learning at TutorialsPoint is fun!!')
},1000)
# this
- arrow functions
don't
create a newthis
pointerinstance
whenever it is invoked
function p(b, c) {
this.b = b;
this.c = c;
this.funcAnonymous = function () {
setTimeout(function () {
//creates a new instance of this , hides outer scope of `this`
console.log(this.b + " " + this.c)
}, 1000)
}
this.fncArrow = function () {
setTimeout(() => {
//uses `this` instance of outer scope
console.log(this.b + " " + this.c)
}, 2000)
}
}
const s1 = new p('Mohammad', 'Mohtashim')
s1.funcAnonymous(); // undefined undefined
s1.fncArrow(); // Mohammad Mohtashim