What are variables?
The variable declaration means creating a variable.
There are 3 ways to declare a variable, using keywords:
// var (a.k.a. VARIABLE) var name; // let let age; // const (a.k.a. CONSTANT) const isMale;
Variable assignment means assigning a value to the variable you declared.
We use the
= symbol to do a variable assignment.
// 1. Declaring variables. var name; let age; // 2. Assign values to the variables we declared. name = 'Yap'; age = 20;
Notice we didn't show an example of using
const. This is because
const has a
different way of using it. We will show this in the
Variable initialization means we declare and assign the value at the same time.
var name = 'Yap'; let age = 20; const isMale = true; // this is how you use `const`
To understand more why we can't declare and assign separately with
Continue on reading...
Variable reassignment means we change the value of a variable to something else.
But, we can't reassign if the variable is declared by using
const means constant (a.k.a. value that doesn't change). Hence, we can only
const variable because it cannot do an assignment.
var name = 'Yap'; console.log(name); // output: Yap name = 'Kaiz'; console.log(name); // output: Kaiz (changed!)
let age = 20; console.log(age); // output: 20 age = 30; console.log(age); // output: 30 (changed!)
const isMale = true; console.log(isMale); // output: true isMale = false; // TypeError: Assignment to constant variable.