Coding Manifestation Logo
Lesson 4

Variable Initialization

Learn how to assign a value to a variable at the time of declaration or later.

Declarationlet age;Memory?Initializationlet age = 25;Memory25

Initialization is the process of assigning an initial value to a variable.

A variable can be initialized at the time of declaration or later in the program.

At Declaration
let name = "Vivek";const PI = 3.14;
After Declaration
let count;count = 10;
Before Initialization
let score;
Memory
undefined
After Initialization
let score = 100;
Memory
100
Bad Example
let age;console.log(age);

Output: undefined (Not recommended)

Good Example
let age = 25;console.log(age);

Output: 25 (Recommended)

  • Forgetting to initialize variables.
  • Using variables before initialization.
  • Assuming uninitialized variables have a value.
  • Confusing declaration with initialization.

Initialize the variables as shown below.

JavaScript
  • Initialization assigns a value to a variable.
  • You can initialize at declaration or later.
  • Uninitialized variables have undefined.
  • Good initialization leads to predictable code.
PrevNext