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

it works but don't understand why return random; was needed

https://w.trhou.se/h6krtc50je

without return random; the code wouldn't had work, but don't understand why it makes it work.

1 Answer

This is your function that returns a random number:

function randomNumber() {
 var random = Math.floor(Math.random() * 256 );
  return random;
}

If you wrote it simply like this:

function randomNumber() {
 var random = Math.floor(Math.random() * 256 );
}

it would assign a random value to the random variable every time, but that's all it would do. It doesn't give you back any value. It simply performs the function.

Using return random is what allows you to get the result of the function that was called.