Variables are containers that store information for later use.
Let's create our first variable:
let test;
We use the let
keyword to declare a variable with the name test
.
But, what happens if we use
console.log()
to log our new variable?let test; console.log(test);
The console prints
undefined
. This is because we have not initialized our variable. Initializing a variable is the process of assigning a value to it.We can assign a value to a variable with the assignment operator
=
let test; test = 1; console.log(test);
- You can also assign a value to a variable directly when you create it.
let test = 1;
console.log(test);
You can also create a variable with the
const
keyword.The difference between the two is that you can not assign a new value to a variable that was created with
const
.const test = 1; test = 2;
we attempt to assign a new value to a variable that was created with
const
.It throws an error.
You also cannot declare a
const
variable without assigning a value to it directly.const test;