DEV Community

Daisy-Chepkwony
Daisy-Chepkwony

Posted on

Arrow Functions

Arrow functions
The arrow function is anonymous function (=>).
syntax example:
let sum = (c+d) =>{
return a+b
}
Arrow functions cannot be used as constructors.
They have a thiskeyword which is a lexical environment,its means its scope is determined by the lexical environment.
example:
const printNumbers = {
phrase: 'The current value is:',
numbers: [1, 2, 3, 4],

loop() {
this.numbers.forEach(function (number) {
console.log(this.phrase, number)
})
},
}

using the this.phrase returns undefined,it shows that the this is anonymous function which is passed to forEach method hence it does not refer to the printNumbers object.

Top comments (0)