
A Place to Put Things
Before `let` or `const` mean anything, here's the simpler question they answer.
Written by
A Lazy Entrepreneur
Before getting into let, const, or any of the syntax, here's a simpler question worth sitting with first.
If JavaScript is going to do something with a value, it needs somewhere to keep that value while it works. So — where does it actually keep it?
That's all a variable is. Not a keyword, not syntax — just a name you give to a value, so you (and the engine) can find it again later.
Variables and Values
let age = 25;age is the name. 25 is the value. The variable doesn't hold the number so much as it points at it — exactly how depends on what kind of value it is, which is a story for another letter.
Naming Things
A variable name has a few rules: it has to start with a letter, _, or $, it can contain numbers after that, and it's case-sensitive.
let userName = "Yasir"; // valid, and readable
let _private = true; // valid
let 2fast = true; // invalid — can't start with a numberBeyond the rules, there's judgment: userAge tells you something. x doesn't. Future-you will thank present-you for the former.
You can also declare more than one variable at once, separated by commas: let a = 1, b = 2; — legal, though most people find one declaration per line easier to read.
One naming convention worth knowing: a const that holds a truly fixed value — one that will never change, like a config constant — is often written in SCREAMING_SNAKE_CASE, e.g. const MAX_RETRIES = 3;. A const that just happens to hold an object or array usually stays in regular camelCase, since it's the binding that's fixed, not necessarily the intent to treat it as a global constant.
Assignment and Reassignment
Giving a variable its first value is called assignment. Giving it a different value later is reassignment.
let score; // declared — no value yet
score = 0; // assignment
score = 10; // reassignmentWhether reassignment is even allowed depends on which keyword you used to declare the variable in the first place — which brings us to the actual choice you make every time you write one.
let
let is for values you expect to change.
let count = 0;
count = count + 1;const
const is for values that shouldn't be reassigned once they're set.
const name = "Yasir";
name = "Someone else"; // ErrorA reasonable default: reach for const unless you already know the value needs to change — then use let.
A Small Word About var
There's a third keyword you'll see in older code: var. It works, but it behaves differently from let and const in ways that used to cause real bugs. Understanding exactly why takes a bit more groundwork — scope, and how JavaScript prepares your code before running it — which is what the next chapter is for. For now, just know it exists, and modern JavaScript mostly leaves it behind in favor of let and const.
Scope, Briefly
Scope means: where in your code can this variable actually be seen? let and const are block-scoped — they only exist inside the { } they were declared in. var is function-scoped — it exists anywhere inside the enclosing function, no matter which block it started in.
if (true) {
let a = 1; // only exists inside this block
var b = 2; // exists for the whole function
}That difference sounds small and causes real bugs in practice — the full treatment, with the bugs it leads to, is coming in the next chapter.
Two more scope-related facts worth knowing now: a top-level var attaches itself to the global object (window, in a browser) — let and const never do this, even at the top level. And if you assign to a name with no keyword at all — no let, var, or const — JavaScript quietly creates a global variable for you, in non-strict code. Neither is something you want happening by accident.
var globalVar = "I'm on window";
console.log(window.globalVar); // "I'm on window"
function oops() {
accidental = "surprise global"; // no let/const/var
}
oops();
console.log(accidental); // "surprise global" — leaked to the global scopeHoisting, Briefly
Before running your code line by line, JavaScript scans it first and sets up your variables in advance — that's hoisting. var gets hoisted and initialized to undefined right away, so reading it early just gives you undefined. let and const get hoisted too, but stay unusable until their declaration line actually runs — reading them early throws an error instead.
console.log(x); // undefined
var x = 5;
console.log(y); // ReferenceError
let y = 5;Why the engine works this way is Chapter III territory. For now, just know the difference exists.
Comparing var, let, and const
| Feature | var | let | const |
|---|---|---|---|
| Scope | Function | Block | Block |
| Reassignable | Yes | Yes | No |
| Redeclarable | Yes | No | No |
| Hoisted as | undefined | Uninitialized | Uninitialized |
| Modern practice | Avoid | Use when value changes | Use by default |
That "Redeclarable" row is worth seeing directly. var lets you declare the same name twice in the same scope without complaint — let/const don't:
var x = 1;
var x = 2; // fine, x is now 2
let y = 1;
let y = 2; // SyntaxError — y is already declaredOne Thing Worth Noticing Already
Not every variable holds its value the same way. Watch this closely:
let a = 10;
let b = a;
b = 20;
console.log(a); // 10
let obj1 = { value: 10 };
let obj2 = obj1;
obj2.value = 20;
console.log(obj1.value); // 20Same pattern in both cases — copy a variable, change the copy. Two completely different outcomes. Why one changed and the other didn't comes down to what kind of value each variable was actually holding — and that's exactly what the next letter is about.
Key Insight
So a variable is just a name pointing at a value. Next question: what kind of value is it actually pointing at?
Study Notes
Before you move on
- Variable
- A name that points at a value; the value itself lives wherever the engine keeps it.
- Naming rules
- Starts with a letter,
_, or$; can include numbers after that; case-sensitive. - Assignment vs. reassignment
- Assignment = giving a variable its first value. Reassignment = changing it afterward.
- let
- For values you expect to change.
- const
- For values that shouldn't be reassigned; the sensible default.
- var
- Older, function-scoped instead of block-scoped, generally avoided in modern code.
- Scope
let/constonly exist inside their{ };varexists throughout the whole function.- Globals
- A top-level
varattaches to the global object (window);let/constnever do. Assigning with no keyword at all creates an accidental global. - Redeclaration
varallows redeclaring the same name in the same scope;let/constthrow aSyntaxErrorif you try.- Hoisting
varis hoisted and set toundefinedimmediately;let/constare hoisted but unusable until their declaration line runs.- Copying values
- Copying a variable doesn't always behave the same way — more in the next letter.
unskilled.pro
Have a thought on this one?
I read every reply. Tell me what you think, what I got wrong, or what you'd want me to figure out next.