Comp 101 Initialization


Initialization gives a previously declared variable a value. Through initialization we are setting a variable equal to a given value.

let x: number; // this step is declaration
x = 999; // this step is initialization

You can combine both declaration and initialization into one line. This just makes your code more concise and easier to read. When you initialize at the same time as declaration, you don't have to specify your variable's type. It will be inferred.

let y = 1101;

Remember: We can only assign variables of the same type.

If we asked, declare and initialize a variable named z to be the number 222. How would you write this in one line of code?