Scope.

What 'scope' is, and how it works in JavaScript.

Firstly, there is no such thing as block scope in JavaScript. It makes JS different from other languages. JavaScript has function scope, i.e. the set of variables, objects, and functions with access to.

Therefore, variables declared within a function become local to the function only and variables with the same names can be used also in other functions. Those local variables are created when a function starts, and are deleted when the function is completed.

Variables declared outside a function, become global. It means that all scripts and functions on a web page can access and change it.

RESOURCES

© Some photos on this blog are linked to iStock Photo NZ and are used for learning purposes. Each photo that belongs to iStock is watermarked. If you'd like to get more amazing photos, please go directly to iStock website.