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

jason limmm
jason limmm
5,535 Points

why 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
Steven Parker
230,248 Points

The 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
jason limmm
5,535 Points

oh, yes i was meant to add the return randnum