JavaScript has different scopes by which a variable can be accessed. In ECMAScript 5, there are two scopes. Globally scoped variables can be read and mutated from any script in the browser, not just the current function. Locally scoped variables can only be accessed from the function in which they were defined.
Global variables should be avoided in most cases. Globally scoped variables make programs more difficult to test, more difficult for other developers to understand, and cause unintended side effects. When a functions uses a global variable instead of a parameter, it is more difficult to create reproducible results. A program with lots of global variables is more difficult for other programmers to reason about because it's not obvious what functions are using a given variable. This increases the difficulty in maintaining the software and adding new features. One function may modi