Technical Blog Part 7. JavaScript scope.

<ul> <li class="question">What 'scope' is, and how it works in JavaScript.</li> <p class="answer"> There are 2 scopes in JavaScript: global and local, and in general scope is the set of variables/functions you can access. <br>If a variable declared outside of a function, it is defined globally, and it means all functions can access it.<br> <br><code>var animal = 'penguin'; // global scope</code><br> <p class="answer">If a variable declared within a function, it is local to the function, and they can only be accessed within the function.<br> <br><code>function findAnimal() {<br> var animal = "ocelot"; // local scope <br>}</code> </p> <p class="answer"> Any function defined inside another function has a local scope which is linked to the outer function, so any variable declared within that function is accessible from any nested functions. Local variables are deleted when the function is completed. </p> </p> </ul>

User's comments

Add a comment