Skip to content

Variable Scope & Hoisting

Programming

Understand variable scope and hoisting to write bug-free code.

Official Site
01

Overview

Scope determines variable visibility. JS hoists declarations: var to undefined, let/const to TDZ. Closures capture enclosing scope variables.

bash
var x=1; let y=2; const z=3;
function f(){var v=1; if(true){var v=2;} return v;}
02

Best Practices

Use let/const over var. Const by default. Minimize globals via modules. Declare at top of scope. Enable strict mode.

Ready to try Variable Scope & Hoisting?

Visit the official site for the latest version and full documentation.

Visit Variable Scope & Hoisting