Boxes for Data
A variable is just a name (label) attached to a memory area where data is stored.
1. const — Your Best Friend
Use const for everything you don't plan to change. This guarantees you won't accidentally overwrite important data.
const API_URL = 'https://api.site.com';
2. let — When Data Changes
Use let if the value will be updated (like a score counter in a game).
let score = 0;
score = score + 10;
3. The Death of var
var is an archaism. It has weird scoping: it ignores { ... } blocks (e.g., inside an if).
Warning
Variables created with var are accessible EVEN BEFORE THEIR DECLARATION (hoisting). This leads to chaos. Never use var in modern code.
4. How to Name Variables?
- Use camelCase (firstWordSmall, OthersCapitalized).
- Names must be descriptive:
userNameis better thanun. - Constants known in advance (before code execution) are often written in UPPER_CASE.
Tip
Always start with const. If you later realize the variable needs to change — the IDE will prompt you to replace it with let. This is the path of a professional.