Introduction
Your code needs to remember things: a price, a name, a count of cups. JavaScript remembers values in variables. Think of a variable as a labelled box: the label is written outside the box, and the value sits inside it. When your code says the label, JavaScript looks inside the box.
JavaScript gives you three words to make a box: let, const, and the old var. They are not equal. In this lesson you will learn the naming rules, see how let values change, feel the lock on const, and learn the simple decision rule professionals use: const first.
(Roman Urdu: Variable aik label wala dabba hai — bahar naam, andar value; const wala dabba lock ho jata hai.)
Key points
- Declaring a variable:
let price = 250;makes a box labelledpriceand puts250inside. The=here means store this value in the box. - Naming rules: a name can use letters, numbers,
_and$— but it cannot start with a number and cannot contain spaces.chaiPriceis fine;2priceandchai priceare errors.(Roman Urdu: Naam hamesha letter se shuru hoga, number se nahi, aur beech mein space nahi aayegi.) - Use camelCase and meaningful names: write
totalBill, nottotalbillortb. A good name tells the reader what is inside the box without opening it. letis for values that change: declare withletwhen the value must move — like a price that rises, or a counter that grows.constis a locked box: declare withconstwhen the value must never change. Trying to store a new value in aconstbox throws a TypeError — JavaScript stops and tells you honestly.(Roman Urdu: const ka matlab hai ye value ab change nahi ho sakti — koshish karoge to error milega.)varis the old way — avoid it:varleaks out of blocks and allows silent re-declaring, which creates surprising bugs. Modern code does not use it.- The decision rule: const first. Use
letonly when you know the value must change. Usevarnever. - Name is not the value: the label
priceis not the number250. The box can hold a new value later (withlet), but the label stays the same.
Code examples
Example 1 — let: a value that changes
let price = 250;
console.log(price);
price = 300;
console.log(price);
let cups = 0;
cups = cups + 1;
cups = cups + 1;
cups = cups + 1;
console.log(cups);What it does: The box price starts with 250, then we store 300 in the same box — the old value is gone. The box cups starts at 0 and grows by one, three times, like a counter counting cups of chai.
What you will see: three lines in the Console: 250, then 300, then 3.
Example 2 — const: the locked box and its honest error
const shopName = "ChaiPoint";
console.log(shopName);
shopName = "Tea House";
console.log(shopName);What it does: shopName is declared with const, so its value is locked at "ChaiPoint". The next line tries to store a new value — and JavaScript refuses, loudly and honestly, instead of silently breaking your program later.
What you will see: the Console prints ChaiPoint, then the script stops with a red error: TypeError: Assignment to constant variable. The last line never runs. That error is const protecting you.
Example 3 — Naming rules in real code
let chaiPrice = 250;
let numberOfCups = 3;
let totalBill = chaiPrice * numberOfCups;
console.log(totalBill);
console.log("Total: " + totalBill + " rupees");What it does: Three variables with clear camelCase names. totalBill is calculated from the other two boxes — 250 * 3 — and printed two ways: as a pure number, and inside a sentence built with +.
What you will see: two lines in the Console: 750 and then Total: 750 rupees.
Quick recap
- A variable is a labelled box: name outside, value inside.
- const first,
letwhen the value must change,varnever. - Names use camelCase, start with a letter, and carry no spaces.
Common mistakes
- Changing a const value — storing a new value in a
constbox throwsTypeError: Assignment to constant variable.Fix: if the value must change, declare it withletfrom the start. - Bad names like
2priceorchai price— names cannot start with a number or contain spaces, and the script fails. Fix: start with a letter and use camelCase, likechaiPrice. - Using var out of habit —
varleaks and re-declares silently, creating bugs that are hard to find. Fix: follow the rule —constfirst, thenlet, nevervar.
Practice
- Declare a
constwith your shop or city name, aletfor a price, and aletcounter starting at0. Increase the counter two times, change the price once, and print all three values. - Deliberately try to change your
constvalue and read the error in the Console. Write one line in your notebook: what does the error say, and why is it helpful?