Local JavaScript Variables(局部变量)

A variable declared (using var) within a JavaScript function becomes LOCAL and can only be accessed from within that function. (the variable has local scope).

You can have local variables with the same name in different functions, because local variables are only recognized by the function in which they are declared.

Local variables are deleted as soon as the function is completed.

Global JavaScript Variables(全局变量)

Variables declared outside a function, become GLOBAL, and all scripts and functions on the web page can access it.

The Lifetime of JavaScript Variables(生命周期)

The lifetime of JavaScript variables starts when they are declared.

Local variables are deleted when the function is completed.

Global variables are deleted when you close the page.

Assigning Values to Undeclared JavaScript Variables(未申明的变量默认为全局变量)

If you assign a value to a variable that has not yet been declared, the variable will automatically be declared as a GLOBAL variable.

This statement:
carname="Volvo";

will declare the variable carname as a global variable , even if it is executed inside a function.

 

发表评论