What are the pros and cons of function-level scope specifically in Javascript compared to block-level scope in languages like Java?
I would like to see examples of function-level scope usage that would be harder or impossible to implement using block-level scope.
The first example that comes to mind is: JavaScript’s handling of closures would be much more expensive if implemented with block-level scope.
When you enter a function in JavaScript, an object is allocated (well, a couple, but we’ll focus on one) that ends up being the “variable object” — that is, where all the arguments and local vars for that function call are held (as properties). It’s this object that the closure actually uses (not just the “symbols” it appears to use; that’s a common misconception). These objects are strung together in a chain, called the scope chain, which is used for resolving unqualified symbols.
Imagine how much more expensive that would be if every block introduced new scope.