TLDR Web Dev · ARTICLES & TUTORIALS

JAVASCRIPT SCOPE HOISTING IS BROKEN

devongovett.me8 min read

Summary from the TLDR Web Dev newsletter

JavaScript scope hoisting is an optimization used by bundlers to concatenate modules into a single scope instead of wrapping each in a function, but it breaks when combined with code splitting because it can't preserve the correct execution order of modules with side effects. This leads to different program behavior, with shared modules executing in the wrong order compared to unbundled code, affecting bundlers like Rollup, ESBuild, and others. The solution requires wrapping modules in functions to control execution order.