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 Introducing the Practice

In this video, the instructor uses what I assume to be a keyword 'let', what does it do?

In this video, the instructor uses what I assume to be a keyword 'let' before declaring the variable to be used in the 'for' loop. In the previous videos as well as in my exercises, the variable was declared right away. What does 'let' do and when should it be used?

Thank you very much for your responses.

2 Answers

Steven Parker
Steven Parker
231,007 Points

The keyword "let" is for defining variables. It is a modern alternative to "var", though there are a few differences.

It is discussed in detail in the first stage of the course on Getting Started with ES2015.

There is also a workshop called Practice Let and Const in JavaScript.

The let keyword is another way of defining your variables but locally in a block code that only works with that block code. In other words, let is used to define variables not globally but locally. Var on the other declares variables globally.

You can find more info from the MDn docs in following link:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let