Now that we have
const, should we still use
Here are some things to think about so you can decide for yourself.
const | part 1
Some people approach this var, let, const thing with a simple rule: “Just always use
const, and if you can’t – then use
let. And don’t ever use
var.” But we’ve found that rules like this usually lead to gaps in understanding. So, just take a little time to think it through. Find some real-world use cases and try them out for yourself.
const | part 2
A lot of people want to think of a variable as a box that holds a value. (and of course we’ve talked about this before…)
But remember how it works:
- A value is stored at some address in memory (pretend a:1)
- A reference is created (a “variable”) (but wouldn’t just calling it a reference be better…)
- The reference is pointed to the place in memory (
reference → a:1)
Now when you reference the reference (the variable) (maybe that’s why they couldn’t call it that) – you’ll be pointing to that value stored at its associated address in memory.
const keyword to assign a variable means it’s tied to that place in memory and can’t be pointed to another. It does not mean you can’t change things about a value stored there…
let | part 1
Get some tea and really spend some time looking at this in detail.
But also, don’t stress about it. You are totally fine to use
var until we find some real-world situations that will help you learn about the differences. Just keep making stuff!