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.