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 Overview

So, just to confirm... Do not use the keyword "var" anymore?

Just wanted to make sure that I heard Andrew Chalkley correctly in this video. var is now basically an obsolete keyword, or at the very least it is not good practice to use anymore, and we should avoid using it at all? Pretty much, only use the const and the let keywords?

6 Answers

andren
andren
28,558 Points

Yes, var is considered obsolete. It continues to exist mainly for backwards compatibility reasons. If you are writing new code intended for browsers compatible with modern JavaScript, then you should always use let over var for potentially changing variables, and const for constant values.

Well explained, thank you Andren.

Jonathan Leack
Jonathan Leack
16,782 Points

Outstanding reply. Thank you for this.

Carl Evans
Carl Evans
11,679 Points

K.I.S.S - I love this answer!

While let, const and the rest of ECMAScript 2015 are certainly the way of the future, it is crazy to tell inexperienced developers -- who by default haven't had enough experience to know the importance of cross-browser compatibility -- not to use var anymore. Please check let's browser compatibility on CanIUse and realize that any visitor to your site using Internet Explorer 10, 9, 8; iOS Safari 9, 8, 7; Mac Safari 9, 8 7; Android's stock browser 4 -- none of these browsers will recognize let or const by default, and all of your site's JavaScript will be broken. If you are going to use ECMAScript 2015 syntax, you must also load in Babel or something else that will compile your new JS syntax back into "old" JavaScript for these still-very-much-in-use browsers.

David Bayley
David Bayley
10,312 Points

So much this. If you are writing code "intended for browsers compatible with modern JavaScript" then you are either in a controlled environment or simply turning your nose up at the general public.

Also just another note on this, unless you are creating your own apps you will likely be going with whatever style guidelines your company or client have established. This can vary depend on how mature and modern the company, the end customer and their tools are. For example, an excerpt from the Google JavaScript Style Guide:

5.1.1 const and let

Declare all local variables with either const or let. Use const by default, unless a variable needs to be reassigned. The var keyword must not be used.

The guide does also outline the requirements for using var in relation to maintaining legacy platforms later in the guide. The preference for your organization maybe something worth addressing during a project kickoff meeting and before code is written or updated.

This will also apply to things like tabs vs spaces in your IDE.

well explained, it seems that people forget about the importance of backward compatibility, and they want to use just modern feature skipping the basic.

Josh Rubner
PLUS
Josh Rubner
Courses Plus Student 6,766 Points

I was surprised when he said this... the earlier part of the Full Stack JS track uses var all the time

Dan McLaughlin
Dan McLaughlin
14,273 Points

The early courses in Full Stack JS are over two years old. They're good, so instead of updating them, they simply added the workshop after them. "No bigs" as the kids say.

Todd Levesque
seal-mask
.a{fill-rule:evenodd;}techdegree
Todd Levesque
Full Stack JavaScript Techdegree Student 7,644 Points

Thank you for adding this section in the course material! Also helpful is Kyle Simpson's book "Scopes & Closures" in chapter 3 he explains more about the differences between function scope and block scope. It is available on github [https://github.com/getify/You-Dont-Know-JS/blob/master/scope%20%26%20closures/ch3.md] "Though some seem to believe so, block scope should not be taken as an outright replacement of var function scope. Both functionalities co-exist, and developers can and should use both function-scope and block-scope techniques where respectively appropriate to produce better, more readable/maintainable code."

Richard Kiddle
Richard Kiddle
9,372 Points

So we should use const and let for all of the Full Stack JS track?

Some of the full stack JS track uses var as it's over 2 years old. If you want you can use const and let but if you are a novice (which looking at your JS point you are not) then i'd recommend just sticking to what the teacher does for a less confusing lesson. :)

Cary Dean
Cary Dean
13,693 Points

Backwards compatibility is too important to start using these keywords without a replacement for the older browsers. Here is a novel idea- if you want your variables to remain constant, don't change them. If you want your vars to change to scope, redeclare them, or better yet, use new vars. This was never really a problem that needed a solution, just like the new arrow notations =>.