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

Barbara Wiacek
Barbara Wiacek
9,901 Points

How would this code look like without the new arrow syntax?

How would this code look like without the new arrow syntax?

fruits.forEach(fruit => console.log(fruit));

3 Answers

Ashley Carpenter
Ashley Carpenter
13,393 Points

Hey, it would like like this:

for (var f in fruit) {
    console.log(f);
}
Barbara Wiacek
Barbara Wiacek
9,901 Points

That would be the for...in statement and not the forEach method, which is what I asked about.

Ashley Carpenter
Ashley Carpenter
13,393 Points

It would have exactly the same result. If you're looking for the exact same prototype function you've used it's

fruits.forEach(function(element) {
    console.log(element);
});

As seen here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

Hi Barbara. This is what it would look like without arrow syntax.

fruits.forEach(function(fruit){
    console.log(fruit);
});
Barbara Wiacek
Barbara Wiacek
9,901 Points

Thanks, I think writing it in the "old" way makes it easier to comprehend what the method is actually doing than using the arrow syntax. The information that I was missing is that the forEach() method executes a function which can take the array element as an argument. Doing it with the arrow syntax may look more intuitive but it is not if you don't know these basics.