User Tools

Site Tools


javascript_global_and_local_variables

Javascript Global and Local Variables

Variables defined inside a pair of brackets are accessible only inside those brackets. They are LOCAL variables.

function myFunction() {
    var a = 4;
    return a * a;
}

Variables defined outside any brackets are global variables, and can be accessed by all functions.

var a = 4;
function myFunction() {
    return a * a;
}

A function can access all variables defined inside the function.

But a function can also access variables defined outside the function.

Variables created without the keyword var, are always global, even if they are created inside a function.

javascript_global_and_local_variables.txt · Last modified: 2018/09/07 18:23 by admin