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

varlevi
varlevi
8,113 Points

Const, let, and var

I've been working through the JavaScript and the DOM course recently, and noticed that Guil used the keywords let and const to create variables, instead of the var keyword. What's the difference between these three, as Guil seemed to use them interchangeably in place of var. Thanks!

2 Answers

let and const are modern replacements for var as ways to declare a variable in JavaScript. They are used in different contexts: once you define a const, its value can't be changed. If you want to have a change-able variable, use let.

"const" is a signal that the identifier won't be reassigned. "let", is a signal that the variable may be reassigned, such as a counter in a loop, or a value swap in an algorithm. It also signals that the variable will be used only in the block it's defined in, which is not always the entire containing function. "const'" and 'let" is a new feature of ECMA-Script 6 and you shouldn't use "var" anymore unless you revise legacy code.