JavaScript Variables

A variable is something that can change ("vary"). Variables can be thought of as "containers" for values used by the programme.

Rules for JavaScript Variable Names: (From: Michael Moncur, 1996, JavaScript 1.1, p31)

Typical Variable Assignments:

age = 52;
MySurname = 'Featherstone';
WeeklyAverage = base * mean / 27;

Variable Scope:

Variables may be either Global, or Local.

Generally, variables declared as "var" are Global variables. Variables declared inside a function as "var" are local to the function. Variables defined inside a function without the "var" are Global variables.

<HTML>
<HEAD>

<TITLE>Variable Scope</TITLE>
<SCRIPT LANGUAGE= "JavaScript">
var count = 22;                           
//GLOBAL
var age = 67;                              //GLOBAL
function audit( ) {
   code = 23;                               
//GLOBAL
   var adjust = 7.5;                       
//LOCAL

   audited = (count * age)/code + adjust;

   return audited;
}

</SCRIPT>
</HEAD>
<BODY>
...
...
...
</BODY>
</HTML>

<- Back - MenuNext ->