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

Return the value right away?

I wonder if there's any difference between storing the value in a const and then returning the const vs returning the value right away.

function getRandomNumber() { const randomNumber = Math.floor( Math.random() * 6 + 1 ) return randomNumber }

vs.

function getRandomNumber() { return Math.floor( Math.random() * 6 + 1 ) }

*EDIT: Fixed an error in the 2nd example, as suggested by Robert.

1 Answer

Robert Manolis
STAFF
Robert Manolis
Treehouse Guest Teacher

Hi Steve, they're basically doing the same thing either way. The one with the variable perhaps has greater readability. But both create and return a random number. And actually, a small adjustment should be made to your second example. Since you're not declaring the randomNumber variable, you would to include that in that second example. Should be like this:

function getRandomNumber() { return Math.floor( Math.random() * 6 + 1 ) }

Hope that helps. :thumbsup:

Hi Robert,

Thanks, that makes sense :)