Variable lifecycle

let a;                  // Declaration
a = 100; // Assignment
console.log(a); // Usage

However, since JavaScript allows us to both declare and initialize our variables simultaneously, so we can declare and initialize at the same time.  

let a = 100;

Note: Always remember that in the background the Javascript is first declaring the variable and then initializing them. It is also good to know that variable declarations are processed before any code is executed. 

However, in javascript, undeclared variables do not exist until the code assigning them is executed. Therefore, assigning a value to an undeclared variable implicitly creates it as a global variable when the assignment is executed. This means that all undeclared variables are global variables.

JavaScript Hoisting

JavaScript Hoisting is the behavior where the interpreter moves function and variable declarations to the top of their respective scope before executing the code. This allows variables to be accessed before declaration, aiding in more flexible coding practices and avoiding “undefined” errors during execution.

Similar Reads

What is Hoisting in JavaScript?

Hoisting is the default behavior in JavaScript where variable and function declarations are moved to the top of their respective scopes during the compilation phase. This guarantees that regardless of where these declarations appear within a scope, they can be accessed throughout that scope....

Features of Hoisting

Declarations are hoisted, not initializations.Allows calling functions before their declarations.All variable and function declarations are processed before any code execution.Undeclared variables are implicitly created as global variables when assigned a value....

Sequence of variable declaration

The following is the sequence in which variable declaration and initialization occur....

Variable lifecycle

let a; // Declarationa = 100; // Assignmentconsole.log(a); // Usage...

Different Examples of JavaScript Hoisting

1. Global Scope...