JavaScript Closures

JavaScript variables can belong to the local or global scope. Global variables can be made local (private) with closures. Global Variables A function can access all variables defined inside the function, like this: Example function myFunction() { var a = 4; return a * a; } Try it Yourself » But a function can also access variables defined outside the function, like this: Example var a = 4; function myFunction() { […]

JavaScript Scope

Scope determines the accessibility (visibility) of variables. JavaScript Function Scope In JavaScript there are two types of scope: Local scope Global scope JavaScript has function scope: Each function creates a new scope. Scope determines the accessibility (visibility) of these variables. Variables defined inside a function are not accessible (visible) from outside the function. Local JavaScript […]