What a variable is
A variable is a named container for a value. The value lives in the computer's memory, and the name is a label you use to find it again. Instead of repeating a value everywhere, you store it once and refer to it by name, which also makes the code easier to read.
Creating and initializing
You create, or declare, a variable with the keyword let followed by a name. You can give it a first value, initialize it, in the same statement with the assignment operator =:
let car = "Toyota";
let experience = 3;
console.log(car, experience);
Toyota 3
Think of each variable as a labelled box: the box car holds the string "Toyota", and the box experience holds the number 3. Wherever you write the name afterwards, TypeScript uses the value inside.
Type annotations
TypeScript lets you state what kind of value a variable holds by writing a colon and a type after the name:
let city: string = "Paris";
let year: number = 2020;
let isOn: boolean = true;
const pi: number = 3.14;
The three basic types are string for text, number for all numbers, whole or decimal, and boolean for true and false. If you initialize the variable, the annotation is optional, because TypeScript infers the type from the value: let year = 2020 is also a number. The lessons use annotations in many places to make the types visible.
Once a variable has a type, the compiler rejects values of another type. Assigning "five" to a variable that holds a number is an error:
error TS2322: Type 'string' is not assignable to type 'number'.
The compiler reports the error, but a type error does not stop it from writing the JavaScript. If n was declared as a number and n = "five" is compiled and run anyway, the program prints five string, because the annotation is gone at run time. Treat the error as a bug to fix, not as something the program will protect you from.
Annotations are checked when the program is compiled and then erased. The JavaScript that runs has no types, so an annotation does not convert a value or protect against a wrong value that arrives at run time, for example from parsed data.
Changing a value: let and const
A let variable can be given a new value later. The new value must match the type:
let total: number = 10;
total = total + 5;
console.log(total);
15
A const variable cannot be reassigned, and the compiler reports Cannot assign to 'pi' because it is a constant if you try. Use const for values that should not change and let for values that will.
Variables without a value
A variable declared without a value holds undefined:
let late;
console.log(late);
undefined
This is almost never what you want. Initialize variables when you create them, so every name always holds a real value.
Naming and a gotcha
Names can contain letters, digits, _ and $, and cannot start with a digit. Capital letters matter, so Age and age are different variables. The usual style is camelCase, as in userName.
The exercise
The exercise creates two variables, one per line:
- Write
let, then the namename, then a colon andstring. - Add
= "Michael", with the value in double quotes, and a semicolon. - On the next line, write
let age: number = 31;. The number goes without quotes."31"would be a string.
The lesson's tests check for these two variables with exactly these names and values.
Common mistakes
- Quoting a number.
let age: number = "31"is a type error. - Misspelling the name later. Using
aniimalfor a variable calledanimalgives an error, because the compiler can't find that name. - Declaring twice. A second
let yearin the same scope is an error. Assign withyear = 2021instead.
Key takeaways
- A variable is a named container for a value.
letdeclares a variable that can change,constone that cannot.- Annotations such as
: string,: numberand: booleandescribe the type and are checked by the compiler only. - Always initialize variables.