Arrow function

Estimated reading time: 2 minutes

.Arrow function is shorthand => . Arrow function represent by arrow => , before arrow declare parameter.

js function

let users = [
  {name: 'kamal', age: 21},
  {name: 'jamal', age: 23},
  {name: 'maruf', age: 7},
  {name: 'sadia', age: 4},
  {name: 'karim', age: 65},
]

users.forEach(function(user) {
  console.log(user.name);
})
// kamal
// jamal
// maruf
// sadia
// karim

arrow function

let users = [
  {name: 'kamal', age: 21},
  {name: 'jamal', age: 23},
  {name: 'maruf', age: 7},
  {name: 'sadia', age: 4},
  {name: 'karim', age: 65},
]

users.forEach((user) => {
  console.log(user.name);
})
// kamal
// jamal
// maruf
// sadia
// karim
  • another example
functionName =  (var1,var2) => {
  return var1 + var2;
}
let result =  functionName(3,2);
console.log(result);
// 5

lexical this

let users = [
  {name: 'kamal', age: 21},
  {name: 'jamal', age: 23},
  {name: 'maruf', age: 7},
  {name: 'sadia', age: 4},
  {name: 'karim', age: 65},
]

this.users.forEach(function(user){
  console.log(user.name);
})

immediately envoke

(() => {
  console.log('Ok');
})()
es, es6, js