Variable Scope
EasyJS Part of EasyProgramming.net - learn about variable scope!
by Nazmus Nasir
HTML
<!-- #7 Scope of Variables - Local vs Global -->
<p>https://jsfiddle.net/easyjs/yoydrj0w/13/#set-as-base
The scope of a variable limits from where you can access the variable. Variables within certain containers, such as objects and functions, are local those containers. Global variables are accessible from everywhere.
</p>
<p>
Article about JS Scope: <a href="http://www.w3schools.com/js/js_scope.asp">http://www.w3schools.com/js/js_scope.asp</a>
</p>
<p>
Visit <a href="http://www.easyprogramming.net">EasyProgramming.net</a>
</p>
JavaScript
//global variable siteName - accessible everywhere!
var siteName = "EasyProgramming";
console.log('siteName outside is ' + siteName);
//function - siteName is still accessible, but topic is only accessible from within the function
(function outputName()
{
console.log('siteName in the function is ' + siteName);
var topic = "JavaScript";
console.log('Topic in the function is ' + topic);
}());
//Topic is NOT accessible
console.log('Topic outside is ' + topic);