<!-- SophiaKnows -->

JAVASCRIPT BASICS
Rev 4: December 2004

<  PART:   A · 1 · 2 · 3 · 4 · 5 · 6 · 7 · 8 · 9  >

 

1. What Are JavaScript Variables?

A variable is a label (or keyname) assigned by the designer-programmer to hold a value for later use.

var variable-label=value;
var demoMessage="Hello World";

Note that use of var in the intitial declaration is conventional but purely optional, and the following would work just as well:

demoMessage="Hello World";

The stored value is then recalled from the variable by invoking the assigned keyname label in a subsequent statement or operation:

document.write(variable-label);
document.write(demoMessage);

This is simple enough. However, there are few general points worth noting here regarding the assignment syntax:

1.1 The Variable Label

The variable label (or keyname) is an unquoted (bareword) string that can include letter (a-z A-Z) and number (0-9) characters. The initial character of the variable-label must be a letter. The variable-label can include the underscore character but not spaces, special characters or punctuation.

1.2 The Assignment Operator

The equal sign (=) acts as the JavaScript assignment operator. This appears straightforward (and is). But just bare in mind that it is the [value] on the right-hand side of the operator is being assigned to the variable [label] on the left.

1.3 The Assigned Value

The assigned value here is a quoted string literal. The value could also be a signed or unsigned integer (-2, 106), decimal value (1.01), another variable label or even the result of another operation or calculation.

Here are assignments using integer values, other keyname labels as well the results of a calculation:

var student_count=63;
var teacher_count=3;
var class_size=(student_count / teacher_count);

A couple of things worth noting: Although they are literal values, integers are assigned to a variable as bare number characters (i.e. without quotations).

Similarly variable labels are referenced and assigned as bare words. I.e.: class_size and "class_size" are not the same thing. The bareword class_size is a variable label. The quoted characters "class_size" make up a string literal.

Listing   1.1 Variables
<script type='text/javascript'>
var student_count=63; 
var teacher_count=3; 
var class_size=(student_count / teacher_count); 
</script>

<p>We are expecting
   <script>document.write(student_count);</script>
   Students to sign up for Intro to Computer Science.

<p>There are
   <script>document.write(teacher_count);</script>
   instructors assigned to teach the Intro course.

<p>Each section will  therefore initially include:
   <script>document.write(class_size);</script>	
   students.

We are expecting Students to sign up for Intro to Computer Science.

There are instructors assigned to teach the Intro course.

Each section will therefore initially include: students.

 

1.4. What Are Datatypes?

As suggested by the examples to this point, values assigned to JavaScript variables can include several "data types" including most commonly:

character strings
numbers (integers and decimal values)
booleans (true | false)
objects (variables)

In many computer languages (e.g. Java, C, Visual Basic) the type of data being assigned to the variable must be explicit when the variable is first declared. JavaScript on the other hand is "loosely typed".

This means the designer programmer does not have to explicitly label the datatypes assigned in his or her program.

This doesn't mean type is completely unimportant. Instead the JavaScript interpreter will attempt to determine the appropriate type from the context in which the value is assigned or used.

1.4.1.2 Strings vs. Numbers

string data is initially assigned as a quoted string literal

number data is initially assigned as a bare word numeric literal

65 is the integer 65. "65" is a string literal including the number characters "6" and "5". This makes a big difference in the following circumstances:

65 + 1 equals 66
"65" + "1" equals "651"

When used with number values, the plus sign (+) acts as the JavaScript addition operator. That is the two number values are alied together to yield a third number value which is returned by the operation.

When used with string characters, the plus sign (+) acts as the JavaScript "concatenation" operator. That is the two string values are joined (concatenated) into a third longer string which is returned by the operation.

Note that when both number and string characters are used in an operation, both will be treated as strings:

"The number is " + 63 = "The number is 63"

In addition, remember that bare (unquoted) words are used to assign, among other things, variable keynames.

 

1.4.1.2 What is Truth?

boolean data are primitive true false conditions. These may be explicitly assigned using the key words true and false

var run = false;
var show = true;

In addition certain literal values and conditions may return a false or true result:

var run = 0; // false
var show = 1; // true
var run = null; // false
var show = (2+1); // true
var run = confirm('run slides?');

But note that we cannot know the results of the last assignment until the user selects [ok] or [cancel]

 

Part 1 of 9 | Next Part 2: JavaScript Arrays >

<  PART:   A · 1 · 2 · 3 · 4 · 5 · 6 · 7 · 8 · 9  >

< CODEBASE | TOP^ | MAINPAGE >

Text & Design By Tony Pisarra
© SophiaKnows 1998-2004