Hoisting
var, let, const, and function declarations
// Function declarations are fully hoisted
console.log(add(2, 3)); // 5
function add(a, b) {
return a + b;
}
// var is hoisted and initialized to undefined
console.log(typeof x); // "undefined" (no error)
var x = 10;
// let is hoisted but in the TDZ
try {
console.log(y);
} catch (e) {
console.log("TDZ error:", e.message);
}
let y = 20;
// Function expression — NOT hoisted
try {
greet();
} catch (e) {
console.log("expression error:", e.message);
}
const greet = function() {
return "hi";
};