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

Brennen Bactista
seal-mask
.a{fill-rule:evenodd;}techdegree
Brennen Bactista
Full Stack JavaScript Techdegree Student 5,523 Points

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