Lesson 3
Variable Declaration
Learn how to declare variables to reserve memory before storing values.
Variable declaration is the process of creating a variable and telling the programming language that the variable exists. It reserves memory for the variable.
After declaration, we can optionally assign a value to it.
1let age;2const PI = 3.14;3var name;Declared Variable
let age;Memory (empty)
Initialized Variable
let age = 25;Memory (with value)
25
Bad Example
age = 25;Error! age is not declared. Declare a variable first.
Good Example
let age = 25;Correct! Variable is declared and initialized.
- Using undeclared variables.
- Redeclaring a variable with const.
- Confusing let and var.
- Forgetting to initialize when needed.
Declare variables as shown below.
JavaScript
- Declaration creates a variable.
- Initialization assigns a value.
- Prefer let and const.
- Avoid var in modern JavaScript.
