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 trialjason limmm
8,009 Pointswhy do i keep making a new variable in my function
so when i call the function, i make a new variable?
function randomnumber(){
const randnum=math.floor(math.random()*6)+1;
}
1 Answer
Steven Parker
231,248 PointsThe keywords var
, let
and const
all create new variables. This function is clearly creating one named randnum
.
But unless more code is added, this won't be useful because the scope (lifetime) of any variable created inside a function is only while the function is running, and this one ends immediately after creating the variable. Did you perhaps intend to also return randnum
in this function?
jason limmm
8,009 Pointsjason limmm
8,009 Pointsoh, yes i was meant to add the return randnum