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 Getting Started With ES2015 Create Functions Using Arrow Syntax Basic Arrow Syntax

gene c
gene c
13,630 Points

So do I use "var" or "let" to declare variables now?

In a previous video in the same track, I was taught to avoid using "var" to declare variables as "var" had some scope issues.

Now this video is using "var" to declare variables. So I am confused.

I generally start with const by default now, but decision between var and let is really about scope, I often use lets inside a loop or a function where the scope is only to that part of the code.

If I need more global scope and a changing variable I will use var.

6 Answers

I generally start with const by default now, but decision between var and let is really about scope, I often use lets inside a loop or a function where the scope is only to that part of the code.

If I need more global scope and a changing variable I will use var.

Steven Parker
Steven Parker
231,248 Points

If you need function scope or hoisting, you'd need "var". But if you rethink your code structure, you can probably change it so neither of these is required. And I personally like using "var" for global variables to visually distinguish them.

Otherwise, using "const" for things that do not change, and "let" for things that do are good practice choices.

Agreed :+1:

tomd
tomd
16,701 Points

You should use 'const'. Unless you need to change the value of a variable later on, then use 'let'.

'Var' is still being used in TreeHouse videos because the videos are old. But they are still relevant.

As I understood, var should be always be avoided from now on...

  • const should be your first option, and if you don't want your variable to be overwritten later on.
  • let is for when te value of your variable is going to be overwritten altogether.
Kevin Gates
Kevin Gates
15,053 Points

To quote Pirates of the Caribbean, "They're more like guidelines than actual rules."

Be wise, but ultimately use the best tools for the job. Remember, at the end of the day, "Done is better than perfect."

farouq siwoku
seal-mask
.a{fill-rule:evenodd;}techdegree
farouq siwoku
Full Stack JavaScript Techdegree Student 7,324 Points

You should always use let when you can i.e use it more often. while var has issues with scope and its generally not in use anymore. You should also use const to declare variables cause it helps to prevent errors.

Steven Parker
Steven Parker
231,248 Points

I think of "const" and "let" as additional (though commonly used) tools in the toolbox, but not replacements.

There are still reasons to use var; it provides function scope and hoisting, the others don't do either. Also, if you use sandbox tools like "codepen", you might find that things declared in the console with "var" work as expected, but you may have problems accessing things declared with "let" or "const".