Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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!