Variables

main > javascript > core javascript > js variables

These tutorials are about JavaScript and its use for ViSTA programming.

Variables

variable: a name, or identifier, associated with a value

A variable is a name, or identifier, associated with a value. The variable can be seen as storing, or holding, the value. What really happens is the identifier refers to a location in memory that stores the value, but in the code we refer to that location by a variable name. This means we don't have to worry about exactly where things are going in memory, merely that they are going to a clearly defined location.

Typing

untyped: in untyped programming languages, variables do not have a type, but take their type from the data type they are holding

Typing is the process of assigning a data type to a variable, which defines what that variable can store. JavaScript is an untyped language. This means that JavaScript variables can hold data of any valid type. It takes its type from the data type of what it is holding.

The following set of statements are a perfectly valid sequence in JavaScript:

var item;     // item is undefined
item = 10;    // item is now an integer
item = 'ten'; // item is now a string

Not only does JavaScript allow variables to take their types from their contents, but except for declaring objects, you cannot declare a type for variables in JavaScript. There is no facility for saying this variable must be a string, or this one must be a floating-point number.

JavaScript also converts types as needed, automatically and behind the scenes.

Assuming the following is a snippet of code, you can track the values and data types through the code:

// set item1 to string '10'
var item1 = '10';

// set item2 to string '20'
var item2 = '20';

// item1 set equal to number 10
// by multiplication, a numbers only process
item1 = item1 *1;

// item2 set equal to string '2010'
// item1 converted to string and concatenated
// concatenation wins over addition
item2 = item2 + item1;

// item2 set equal to number 201
// division is a numbers only process
item2 = item2 / item1;

If you want to specifically convert variables to numeric values, JavaScript also has the parseInt( ) and parseFloat( ) functions. These functions accept a string variable and if it begins with a number, create a numeric value of that number.

For instance:

var string1 = '3.1417 is the value of Pi';
var string2 = 'The value of Pi is 3.1417';

// parseInt on the two strings
Int_1 = parseInt(string1);  
// value of Int_1 is 3

Int_2 = parseInt(string2);  
// value of Int_1 is NaN

// parseFloat on the two strings
Int_3 = parseFloat(string1);  
// value of Int_1 is 3.1417

Int_4 = parseFloat(string2);  
// value of Int_1 is NaN

Of course, since JavaScript allows for dynamic data type conversion, you can just use the following fast and dirty tricks.

// convert string to number 
// multiply by 1
numVal = strVal * 1;

// convert number to string
// add empty string
strVal = numVal + '';

Not that such behavior is not recommended. It is merely allowed.

The Declaration Statement

You declare variables in JavaScript with the var keyword.

You can declare multiple variables at once. You can also declare a variable and assign it a value at the same time. Until you assign a value to a variable it is undefined, which means it has no value assigned to it.

In order to use a variable it needs to be declared. In order to use the value of a variable, it needs a value defined for it.

// simple declaration
var item1;

// declaration of multiple variables
var item1, item2, item3;

// declaration with assignment
var item1 = 7;

// multiple variables with assignment
var item1 = 7, item2 = 'cat', item3 = 3.17;

If you try to declare a variable that already exists, JavaScript will treat it as a simple assignment statement and assign any new value in the declaration statement to the variable. If the duplicate declaration has no assignment, then nothing happens.

If you try to assign a value to a non-existent variable, JavaScript will create the variable for you. This is called implicit declaration. Implicitly defined variables are always treated as global variables.

This can be a problem when declaring variables within functions. Unless you are working with really simple scripts, it is a good idea to always explicitly declare your variables with the var statement.

Scope

scope: is the region of the program for which the variable is declared

All variables have scope. The scope is the region of the program for which the variable is declared. JavaScript has two levels of scope, global and local.

A global variable is one that is defined everywhere in your script, as well as, by extension, everywhere in the document containing the script. All predefined variables and objects are global in scope, as are implicitly declared variabled and variables that are defined outside of functions.

A local variable is one that is local to a function. In other words, it only exists within the function for the duration of the function.

Unlike some other programming languages, JavaScript has no block scope. Block scope is a scope that only applies to a single compound statement within the code, such as a loop or conditional. JavaScript does not recognize block scope, so anything declared within a code block applies throughout the function or document, depending on where the block of code appears.

The following code with print 'local' twice, which is perhaps not what we intended.

var item1 = 'global';

function testTheScope( ) {
  item1 = 'local';
  document.write(item1);
  }

testTheScope( );
document.write(item1);

The following code with print first 'local' from inside the function, and then 'global' after the function runs. The only difference is the explicit declaration of the variable inside the function, creating a local variable instead of a global one.

var item1 = 'global';

function testTheScope( ) {
  var item1 = 'local';
  document.write(item1);
  }

testTheScope( );
document.write(item1);