DEV Community

Discussion on: ES6 Arrow Functions Cheatsheet

Collapse
 
gmartigny profile image
Guillaume Martigny

Like a lots of things in JS, you can screw yourself if you're not careful with the flexible syntaxe.

ESlint (with airBnB rules) can help a lot with this. As you said, there's two (maybe three) ways to use arrow-functions. Here's the two syntaxes I enforce to myself.

1 Simple one-liner:

this.children.forEach(child => child.remove()); // Without implicit return
const translated = points.map(point => point.add(x, y)); // With implicit return

1.1 Object return:

const options = values.map(value => ({
  value: value,
  size: value.length,
}));

2 Multi-line operation:

object.data.forEach((data) => {
  test(data.value);
  test(data.size);
});