Picture some JavaScript which consisted of two functions:

function 1 {

}

function2 {

}

There are three places you could declare a variable:

  1. outside of the two functions
  2. inside function 1
  3. inside function 2

Depending where you did that you would be able to use the variable in different places:

  1. everywhere (outside the functions and inside both)
  2. just inside function 1
  3. just inside function 2

Number 1 is known as a global variable (available globally).  The other two are local variables (local to the function).

If you understood that then move on to the next page by pressing the right arrow above unless you are on a qualification course which requires you to explain the difference.  If you need more help or an example for your course do the bit below.

Further explanation

The code below looks complicated but is basically doing this:

function 1 {

    code to display if the variables exist here

}

function2 {

    code to display if the variables exist here

}

    code to display if the variables exist here

Create a page called scope.html and put this code in it:

var thisVariable='I am a global variable';
function someFunction(){
    var someVariable=1;
    if (typeof thisVariable != 'undefined'){
        alert('thisVariable inside the first function is: '+thisVariable);  
    } else {
        alert('thisVariable does not exist inside the first function');
    }
    if (typeof someVariable != 'undefined'){
        alert('someVariable inside the first function is: '+someVariable);  
    } else {
        alert('someVariable does not exist inside the first function');
    }
    if (typeof otherVariable != 'undefined'){
        alert('otherVariable inside the first function is: '+otherVariable);  
    } else {
        alert('otherVariable does not exist inside the first function');
    }
}

function otherFunction(){
    var otherVariable='fred';
    if (typeof thisVariable != 'undefined'){
         alert('thisVariable inside the second function is: '+thisVariable);  
     } else {
         alert('thisVariable does not exist inside the second function');
     }
     if (typeof someVariable != 'undefined'){
         alert('someVariable inside the second function is: '+someVariable);  
     } else {
         alert('someVariable does not exist inside the second function');
     }
     if (typeof otherVariable != 'undefined'){
         alert('otherVariable inside the second function is: '+otherVariable);  
     } else {
         alert('otherVariable does not exist inside the second function');
     }
}
	    
if (typeof thisVariable != 'undefined'){
	alert('thisVariable outside the functions is: '+thisVariable);	
} else {
	alert('thisVariable does not exist outside the functions');
}
if (typeof someVariable != 'undefined'){
	alert('someVariable outside the functions is: '+someVariable);  
} else {
    alert('someVariable does not exist outside the functions');
}
if (typeof otherVariable != 'undefined'){
	alert('otherVariable outside the functions is: '+otherVariable);  
} else {
    alert('otherVariable does not exist outside the functions');
}

someFunction();
otherFunction();

The last two lines call the functions (make them run).  The code outside of the functions should run first and then the two functions.  Try the page and try to follow why each dialog says what it does.