Middle
What is hoisting?
sobes.tech AI
Answer from AI
Hoisting is a mechanism in JavaScript where variable and function declarations are moved to the top of their scope during the compilation phase.
This means that variables declared with var and functions can be used before their actual declaration in the code, although for var variables, the value will be undefined until assigned.
Example:
console.log(a); // undefined
var a = 5;
foo(); // Will output 'Hello'
function foo() {
console.log('Hello');
}
Variables declared with let and const are not hoisted in the same way, and calling them before declaration will result in an error.