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

why use `let` when `var` is working pretty well ?

var book = {
  title : 'Black hat python',
  author: 'Justin Seitz',
  publish_year: 2015,
};

for (var key in book){
  console.log(key, ': ',book[key]);
}

2 Answers

Steven Parker
Steven Parker
231,172 Points

It wouldn't make much difference for the definition of "book", but an advantage of "let" for the loop variable is that the scope is limited to within the loop. So if there happens to be another variable with the same name in the wider scope, this prevents a conflict.

Thx