When (to and not to) use ES6 Arrow functions in JavaScript?

It is important to note that arrow functions are anonymous, which means that they are not named.

This anonymity creates some issues:

Main benefit: No binding of ‘this’

When you should not use Arrow Functions

var cat = {
lives: 9,
jumps: () => {
this.lives--;
}
}
var button = document.getElementById('press');
button.addEventListener('click', () => {
this.classList.toggle('on');
});

When you should use them

--

--

Developer with 3 yrs of industrial experience in developing scalable web applications.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Abhay Jain

Developer with 3 yrs of industrial experience in developing scalable web applications.