Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Basic Arrow Syntax

Would you recommend arrow syntax?

Would you recommend using arrow syntax, when using iterator methods such as .forEach() .map() and .filter() ?

It is a preference thing, but I prefer the arrow syntax at it keeps the code cleaner. Especially when you have several .map() stacked it can keep your code more concise. and you are dealing with objects

//students -> array of student
//example student -> {name: 'alex', age:25, location: 'TX' }

const ages = students.map(student => student.age);
const names = students.map(student => student.name);
const locations = students.map(student => student.location);


//without arrow syntax
const ages = students.map(function(student) {
        return student.age;
});
.
.
.

2 Answers

Steven Parker
Steven Parker
231,198 Points

It's important to be aware of the limitations of arrow functions: they do not have their own "this", "arguments", "super", or "new.target". And they cannot be used as constructors or generators.

But otherwise, I prefer them because they provide a more concise definition format, particularly for anonymous callback arguments.

For more information, see the MDN page on Arrow Functions.

Thank you very much for the answers, it was very helpful :)