Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their scope before code execution.

One of the advantages of JavaScript putting function declarations into memory before it executes any code segment is that it allows you to use a function before you declare it in your code. For example:

function print(name) {
console.log("My name is " + name);
}

print("Abhay");

/*
The result of the code above is: "My name is Abhay"
*/
print("Abhay");function print(name) {
console.log("My name is " + name);
}

/*
The result of the code above is: "My name is Abhay"
*/

JavaScript only hoists declarations, not initializations.

console.log(num); // Returns undefined, as only declaration was hoisted, no initialization has happened at this stage
var num; // Declaration
num = 6; // Initialization
console.log(num); // Throws ReferenceError exception
num = 6; // Initialization
// Example with let:
a = 1; // initialization.
let a; // Throws ReferenceError: Cannot access 'a' before initialization

// Example with const:
a = 1; // initialization.
const a; // Throws SyntaxError: Missing initializer in const declaration

Developer with 3 yrs of industrial experience in developing scalable web applications.