Learn programming concepts interactively
// Function Declarationconsole.log(square(5)); // 25function square(n) {return n * n;}
// Function Expressiontry {console.log(square(5));} catch (e) {console.error(e.message);}const square = function(n) {return n * n;};
Function declarations are moved to the top of their containing scope during the compilation phase. This is why you can call them before they appear in the code.
// How JavaScript sees it (Hoisting)function square(n) {return n * n;}console.log(square(5));
Variables declared with `const` (and `let`) are also hoisted, but they are not initialized. They are in a "temporal dead zone" from the start of the block until the declaration is encountered. This is why you get a `ReferenceError` when trying to access a function expression before it's defined.