Home Articles FAQs XREF Games Software Instant Books About Feedback Search Site-Map
irt.org logo

Q1321 Can you explain the scope of global and local variables in relation to functions?

irt.org | Knowledge Base | JavaScript | Function | Q1321 [ previous next ]

Q1321 Can you explain the scope of global and local variables in relation to functions?

Take for example the following code:

<script language="JavaScript"><!--
// declare global variable
var variable1;

// declare and initialise global variables
var variable2 = '2';
variable3 = '3';

document.write('1 = ' + variable1 + '<br>');     // 1 = undefined
document.write('2 = ' + variable2 + '<br>');     // 2 = 2
document.write('3 = ' + variable3 + '<br>');     // 3 = 3
document.write('6 = ' + variable6 + '<br>');     // 6 = undefined
document.write('6 = ' + variable6 + '<br>');     // JavaScript Error: variable6 is not defined.

function myFunction() {
    // declare and initialise local variables
    var variable1 = '1 amended';
    var variable2;

    // initialise global variable value
    variable3 = '3 amended';

    // declare local variable
    var variable4;

    // declare and initialise local variable
    var variable5 = '5';

    // declare and initialise global variable
    variable6 = '6';

    document.write('1 = ' + variable1 + '<br>'); // 1 = 1 amended
    document.write('2 = ' + variable2 + '<br>'); // 2 = undefined
    document.write('3 = ' + variable3 + '<br>'); // 3 = 3 amended
    document.write('4 = ' + variable4 + '<br>'); // 4 = undefined
    document.write('5 = ' + variable5 + '<br>'); // 5 = 5
    document.write('6 = ' + variable6 + '<br>'); // 6 = 6
    document.write('7 = ' + variable7 + '<br>'); // 7 = undefined
    document.write('8 = ' + variable8 + '<br>'); // 8 = undefined

    // declare local variable
    var variable7;

    // declare and initialise local variable
    var variable8 = '8';

    document.write('8 = ' + variable8 + '<br>'); // 8 = 8
    document.write('9 = ' + variable9 + '<br>'); // JavaScript Error: variable9 is not defined.
}

myFunction();

document.write('1 = ' + variable1 + '<br>');     // 1 = undefined
document.write('2 = ' + variable2 + '<br>');     // 2 = 2
document.write('3 = ' + variable3 + '<br>');     // 3 = 3 amended
document.write('4 = ' + variable4 + '<br>');     // JavaScript Error: variable4 is not defined.
document.write('5 = ' + variable5 + '<br>');     // JavaScript Error: variable5 is not defined.
document.write('6 = ' + variable6 + '<br>');     // 6 = 6
document.write('7 = ' + variable7 + '<br>');     // JavaScript Error: variable7 is not defined.
document.write('8 = ' + variable8 + '<br>');     // JavaScript Error: variable8 is not defined.
document.write('9 = ' + variable9 + '<br>');     // JavaScript Error: variable9 is not defined.
//--></script>

If we declare but not initalise a variable outside a function it should be global in scope, but undefined until the point at which it is initialised - e.g. variable1.

If we declare (with or without the var keyword) and initialise a variable outside a function it should be global in scope and accessible immediately - e.g. variable2 and variable3

If we declare (and optionally initialise) a variable with the var keyword inside a function, even though a global variable of the same name exists outside of the function, then it should be local in scope and not effect the value of the global variable - e.g. variable1 and variable2.

If we declare and initialise a variable without the var keyword inside a function, if a global variable of the same name exists outside of the function, then it should effect the value of the global variable, and still remain global - e.g. variable3.

If we declare but not initalise a variable with the var keyword inside a function it should be local in scope, but undefined across the complete function until the point at which it is initialised - e.g. variable4 and variable7.

If we declare and initialise a a variable with the var keyword inside a function it should be local in scope and accessible immediately - e.g. variable5.

If we declare and initialise a variable without the var keyword inside a function it should be global in scope, but not accessible prior to the function being invoked - e.g. variable6.

If we declare and initalise a variable with the var keyword inside a function it should be local in scope, but undefined across the complete function until the point at which it is initialised - e.g. variable8.

Feedback on 'Q1321 Can you explain the scope of global and local variables in relation to functions?'


Provide feedback ...
AddThis Social Bookmark Button

Provide feedback ... AddThis Social Bookmark Button


Last Updated: 30th March 2008. Maintained by: Martin Webb and Michel Plungjan
irt.org liability, trademark, document use, privacy statement and software licensing rules apply.
Copyright © 1996-2008 irt.org, All Rights Reserved.