While you declare a variable in a function, the variable can be accessed within that function only. While you exit the function, the variable is destroyed. These variables are called as local variables. You can have local variables along the similar name in distinct functions, because each is identified only by the function wherein it is declared.
If you declare any variable outside a function, all of the functions on your page can access it. The lifetime of these variables begins while they are declared, and ends while the page is closed.