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 JavaScript Functions Arrow Functions Function Challenge Solution

Kara Caputo
Kara Caputo
2,213 Points

Does const need a different name than function name?

My function appears to work fine when I call it in the console, but I noticed in the solution video the example function has a different name than the constant, whereas I named mine the same. Does this make a difference? Is there a reason to give them different names?

function randomNumber(lowNumber, highNumber) {
  const randomNumber = Math.floor(Math.random() * (highNumber - lowNumber + 1)) + lowNumber; 
  return randomNumber;
}

3 Answers

In this particular case, it doesn't matter, but I would still advice against it mainly for these two reasons:

  1. code clarity. Names should be as descriptive as possible. As a general rule function names should be verbs if the function changes the state of the program, and nouns if they're used to return a certain value. So in your case I'd suggest "generateRandomNumber()". Don't worry about long names, it's better to have a long name that explains clearly what the constant or function does, than to have a short and confusing name. This will also avoid naming conflicts.

  2. The reason randomNumber is correctly recognized in this case is because the fact that you redefined it within the function itself means that the inner scope of the function "beats" the outside scope of the function itself. To demonstrate:

const example = "this is an example string";
function doSomething() {
  console.log(example); // this will print "this is an example string" because a local const with this name doesn't exist yet so it looks in the outer scope.
  const example = 5;
  console.log(example); // this will print 5, because a new locally scoped example was defined. Since it has a different scope, it will work fine.
}
const example = "this will throw an error"; // this will throw an error because it's in the global scope and a const already exists in this scope, so it conflicts.

but seperating values only by scope can quickly become confusing and isn't good practice. Especially if you move around / reorganize code it can mess up the behavior of your code and lead to unexpected results.

Mark Casavantes
Mark Casavantes
10,619 Points

Good Morning Kara,

When computers were new I had a computer that had no passwords or protections to the operating system. My daughter would name all the files to her name. The computer would no longer work until I reloaded the operating system. I tried to explain why it was not a good idea to my daughter. She repeatedly named everything with her name. My gut feeling is that it is not a good idea to name functions and variables with the same names.

Maybe someone can elaborate.

Thank you,

Mark

Harald N
Harald N
15,843 Points

I tested your code to see if it would throw any types of Errors, and it din't. Therefor it seems like it is not a "Problem" however I don't think this goes under "Best practise". The code still works, but it is unclear what you are returning unless you read the code(especially bigger functions) and therefor naming it just "number" would make it more what is what, and what you are returning.

I didn't get any Error, but I don't know if it would throw an Error in another environment. Hope, this helps a bit.