The behaviour is due to the fact that variables in JavaScript are scoped to the function, although as that code is in the global scope and lacks a var declaration it will live under the global object (window in this case). So in that example each closure captures the same variable hence 4 being produced within each alert(). For more info see: https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/C...
https://shadycharacters.co.uk/books/shady-characters-the-boo...
He also posts, with some regularity, to his blog on the same topic:
https://shadycharacters.co.uk/
Employing the correct usage of hyphens, en dashes and em dashes continues to be a small daily joy for me.