Avoid creating global variables unless absolutely necessary. Your global variables (or functions) can potentially overwrite existing properties or functions of the window object. Likewise, any function, including those of the window object, can overwrite your global variables and functions. |
A JavaScript variable’s lifetime begins when it is declared.
Local (function) variables are deleted once the function finishes executing.
In a web browser, global variables are deleted when the browser window or tab is closed.
Function arguments (parameters) behave like local variables within functions.