ES6 Arrow functions in 5 minutes

Hashir Hussain
Sep 6, 2018 · 1 min read

Arrow functions are a shorthand for anonymous functions in JavaScript. Unlike function, arrows share the same lexical `this` as their surroundings bodies.

Expression bodies

var result = items.map(v => v + 1);var result = items.map((v, i) => v + i);var result = items.map((v, i) => ({item: v, key: i}));

Statement body

var result = [];
items.forEach(item => {
if(item % 2 === 0){
result.push(item);
}});

Lexical body

var student = {
name: 'Hashir',
subjects: [],
printSubjects() {
this.subjects.forEach(sub => {
console.log(`${this.name} knows ${sub}`);
});
}
};
student.subjects = ['Math', 'Science', 'English'];
console.log(student.printSubjects());
/*print:
Hashir knows Math
Hashir knows Science
Hashir knows English
*/

Good luck and feel free to ask queries :)

Written by

MEAN/MERN Stack Developer

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade