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 JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Using For Loops with Arrays

How come the printList(list) function doesn't have an explicit return defined?

Is the return value implicit if it's not explicitly defined? When I start creating a function, I want to remember to include its return value. How come there's not one here?

function printList( list ){ var listHTML = "<ol>"; for(var i = 0; i < list.length; i += 1 ) { listHTML += "<li>" + list[i] + "</li>"; } listHTML += "</ol>"; print(listHTML); }

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! Not every function needs a return statement. Sometimes we need to send back the results of our work, and sometimes we don't. In this example, it's building up a string it names listHTML. At the end of the function, it calls yet another function print and sends the string to it. This function accepts the string coming in, so the data is not lost between them. So no return is needed here.

Hope this helps! :sparkles:

Waffles Norton
seal-mask
.a{fill-rule:evenodd;}techdegree
Waffles Norton
Front End Web Development Techdegree Student 392 Points

Jennifer is correct that not all functions need to have a return. That said, you could make this function for flexible and more usable across other parts of your program by having it return listHTML. Then you could call the function and save the result into a variable and use the ol however you want in the future. It really is up to you and your code!