Para entender "var" y su alcance, al igual que "let" y "const".
https://developer.mozilla.org/es/docs/Web/JavaScript/Reference/Statements/var https://developer.mozilla.org/es/docs/Web/JavaScript/Reference/Statements/let https://developer.mozilla.org/es/docs/Web/JavaScript/Reference/Statements/const