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 Simplify Repetitive Tasks with Loops The Refactor Challenge, Part 2

When do I call return?

In this video he said write a function print(message)

but why doesnt he return?

Why other functions like randomColor return color and function randomNumber?

1 Answer

Justin Cantley
Justin Cantley
18,052 Points

You would want to return from a function if you want to call the function in order to assign the return value to a variable. For instance:

function add(x, y) {
    let sum = x + y;
    return sum;
}

let result = add(4, 5); //by calling the add() function here it will return 9 and assign it to result

In the case of calling the print() method, you would not need to return the value, because the print() method performs an action (document.write()) instead of returning a value.

:smile: ty

Justin Cantley
Justin Cantley
18,052 Points

No problem. I hope this helped. The following links have some helpful documentation if you want to know more.
W3 Schools
MDN