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 Using Next-generation ES Features

why did he use var in the second function and not const or let.

why did he use var in the second function and not const or let.

they say (other courses) that var must only be use if you don't have any other means.

1 Answer

Steven Parker
Steven Parker
243,656 Points

I don't understand what your other source meant by "if you don't have any other means", but I suspect I would disagree with it even if I did. I often choose "var" to give a variable function scope or to take advantage of hoisting. And while it may not be a common convention, I also like to use it for global variables to visually identify them in the code.

In this particular case, the scope of the variable is the same for "let" or "var", and the protection offered by "const" isn't needed since the variable is used once and immediately disposed. So there's really no practical difference between any of them here, though the modern "best practice" convention would be to use "const". I would actually write this function without using any variable at all:

async function myAsyncFunction() {
  console.log(await doubleAsync(1337));
}