Lessons · TypeScript · storing a value in a name
Naming a value
const name = value stores a value under a name so you can use it again; let is for names that will be reassigned.
Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.
What it is for
Every program is values with names. Choosing const by default tells the reader which names never move.
How to think about it
Will a later line reassign this name? Name the thing for what it means, not what it is: unitPrice, not x. Use const unless a later line reassigns it.
Worked example
const unitPrice = 2.5;Never reassigned.
let qty = 3;Will change.
qty = qty + 1;Reassignment: only allowed because qty is let.
const cost = unitPrice * qty;10.
Your turn
Store a greeting under a name.
greeting = "hello";
Solve one with the tests running
The trap
var: function-scoped and hoisted, the source of many old bugs. Do not use it.
Practise storing a value in a name on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.