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 JavaScript Numbers The Math Object Random Number Challenge – Two Numbers Solution

Deontra Thomas
seal-mask
.a{fill-rule:evenodd;}techdegree
Deontra Thomas
Full Stack JavaScript Techdegree Student 1,513 Points

Different times to use var, let and const and why is const preferred mostly in tutorials?

Just for better understanding although some can be interchangeable or is this personal preference.

1 Answer

Great question!

It's awesome that you're already curious about the difference between "const", "var", and "let".

First of all, "const" is a variable that can only be changed where you set it. In other words, it is harder to unintentionally change "const" as you code (you will normally use this at the beginning of your code to set global variables). Think of it as a somewhat permanent variable. As for "var" and "let" they both make it easier to change than "const". The only difference is the scope in which "var" and "let" belong. The "var" variables (which I have rarely used) belongs to either the global or local scope, and "let" variables (used a lot in functions) belong to the block scope. If you still find it hard to understand, do not worry, I'm confident you will get a better understanding of it as you go.

Also, feel free to reference: https://dev.to/sarah_chima/var-let-and-const--whats-the-difference-69e