Variables declared within a JavaScript function, become LOCAL to the function.

Local variables have local scope: They can only be accessed within the function.

// code here can not use carName

function myFunction() {
    var carName = "Volvo";

    // code here can use carName

}

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.

Automatically Global

If you assign a value to a variable that has not been declared, it will automatically become a GLOBAL variable.

myFunction();

// code here can use carName 

function myFunction() {
    carName = "Volvo";
}

Do NOT create global variables unless you intend to. In “Strict Mode” automatically global variables will fail.

In HTML, the global scope is the window object. All global variables belong to the window object.

var carName = "Volvo";
window.carName // "Volvo"

Yonggoo Noh

I am interested in Computer science and Mathematics.