Local variables have local scope: They can only be accessed within the function.
Since local variables are only recognized inside their functions, variables with the same name can be used in different functions.
Local variables are created when a function starts, and deleted when the function is completed.
A variable declared outside a function, becomes GLOBAL.
A global variable has global scope: All scripts and functions on a web page can access it.
If you assign a value to a variable that has not been declared, it will automatically become a GLOBAL variable.
This code example will declare a global variable carName, even if the value is assigned inside a function.
Do NOT create global variables unless you intend to.
Global Variables in HTML
In HTML, the global scope is the window object. All global variables belong to the window object.
Did You Know?
Your global variables (or functions) can overwrite window variables (or functions). Any function, including the window object, can overwrite your global variables and functions.
Local variables are deleted when the function is completed.
Global variables are deleted when you close the page.
Function arguments (parameters) work as local variables inside functions.