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 Refactor Arrow Function Expressions

Jesse Gay
seal-mask
.a{fill-rule:evenodd;}techdegree
Jesse Gay
Full Stack JavaScript Techdegree Student 14,045 Points

Why don't we need return keyword for printToTen()? Function has more than one line.

const printToTen = () => {
    for (let i = 1; i <= 10; i++) {
        console.log(i);
    }
}

has more than one line in the body. So how come we don't need the return keyword? Is it because, despite the fact that there are multiple "lines" when we view it as-written, there is really only 1 line (as far as JavaScript is concerned) because there is only 1 semicolon.

2 Answers

Steven Parker
Steven Parker
231,198 Points

A "return" is implied, and therefore optional, at the end of every function. It's only mandatory when the function needs to pass back a value to the caller, which this one does not.

Semicolons at the end of statements are also optional, but it is considered a "best practice" to include them.

Jesse Gay
seal-mask
.a{fill-rule:evenodd;}techdegree
Jesse Gay
Full Stack JavaScript Techdegree Student 14,045 Points

Thanks Steven Parker . Ah, got it. I was a little confused since at 00:56 of https://teamtreehouse.com/library/convert-to-arrow-function-expressions Ashley says "we also don't need the return keyword since arrow functions implicitly return the value of one-line functions." But, as you point out, this one doesn't return anything, so it's a moot point.

Steven Parker
Steven Parker
231,198 Points

The special one-line form of an arrow function does return a value, but this isn't one of those.

Happy coding!