JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops A Closer Look at Loop Conditions

Rouillie Wilkerson
Rouillie Wilkerson
8,140 Points

Let vs Var...

I'm supplementing my Javascript learning with various other sources. One consistency is a preference for "let" instead of "var". I see "var" used here a lot. When should I use let?

1 Answer

Steven Parker
Steven Parker
171,342 Points

Some of the videos have not been revised for a while. Since ES6 (2015), "let" and "const" have become the common choices for variable declarations. "Const" is preferred when possible, and "let" if the variable will be changed.

Just be aware that they both have block scope, and are not hoisted. Use "var" if you need hoisting or function scope (but with good planning, neither should be necessary).

I'll admit that personally, I still like to use "var" for global variables just as a way to distinguish them at first glance.

Rouillie Wilkerson
Rouillie Wilkerson
8,140 Points

Thank you, I understand some of your response, but you've given me more reading to do! That's a good thing!

Steven Parker
Steven Parker
171,342 Points

Rouillie Wilkerson — Glad to help. You can mark a question solved by choosing a "best answer".
And happy coding!