Why Does var Print 2 Twice? Unraveling JavaScript’s Function Scope and Hoisting
This article explains why a JavaScript function using var prints “2” twice, by detailing var’s function scope, variable hoisting, the compilation and execution phases, and contrasting the behavior with let and const, while offering practical coding recommendations to avoid common pitfalls.
