Variables with let and const
A variable is a named box that holds a value.
TypeScript uses the same let and const as JavaScript.
Each name also has a type name tag.
Start with const. Use let only when the value must change.
const for names that stay put
const stops you from giving that name a new value.
The box keeps the same toy.
For objects and arrays, the insides can still change.
An object is a bag of named values.
An array is a list.
let when the value changes
Use let for counters, flags, and anything you reassign.
A flag is a yes or no switch.
TypeScript still locks the type after the first value.
Or after you write a type yourself.
Widen carefully with types
Widen means open the name tag to allow more values.
Without a type, a string may be treated as that exact word.
Writing string opens up what you can assign later.
A union opens it to a few choices. We cover that soon.
Skip var
var is older.
It can surprise you with how it scopes.
Scope means where a name can be seen.
TypeScript still allows var so old code works.
Stick to let and const in new code.
Name things clearly
Pick names a friend can read.
Use camelCase for variables.
camelCase means firstWordThenNextWords.
Default to const. Use let when you need to reassign.
Test yourself
Three quick questions made just for this lesson. Earn 10 XP per correct answer.