Variables


Variable Types

So... You're asking yourself what types of variables does Javascript allow?
Okay so maybe you're not thinking it, but let's pretend.

Numbers (e.g. 123 321 132 231 312 etc...)
Logical Values (e.g. true false)
No fuzzy logic yet...
Strings (e.g. "Hello World", "I love Javascript!")
Null (e.g. null)
What you wanted more?!

Variable Names

So.. Now that we know what type variables you can have, is there a limitation on what we can call the variable? But of course. In order for Javascript not to throw a weird error out on execution, you must use variable names that begin with a letter or underscore ("_"); numbers just don't cut it! This is mainly because if you start to use numbers, Javascript will assume that the rest of your "word" is a number. The only letters you can use are "A" through "Z" (upper & lower case). No funky high ascii or spaces, please. Oh, by the way, Javascript is case-sensitive so Coolvariable1 is not the same as CoolVariable1 [Note the capitalized "V"].

Variable Values

Although explanation of variable values are not need, some attention must be given to strings. The main problem occurs when you are programming away and all of a sudden you need to use a quote within your string. What can one do?! Well, think of another method of doing what you want without using quotes, or take the lazy man's method and "escape the quotes" with your friend and mine, the backslash ("\").

ex: "this is a string value with \"quoted\" material in it"
In addition, you can aslo use other commonly escaped unix-type stuff like:

\b Backspace
\f Form feeds
\n New line
\r Carriage return
\t Tab
Variable Syntax

   [var](variable name) = (variable value);
A few notes:
Here are a few examples:
 var thisvariable = false;
 var thatvariable = 1;
 var thosevariables = "two";
 var novariables = null;

Arrays


Arrays are variables that can hold a lot of different values. You can use brackets to specify which element in the array you wish to reference. Example:
  student[0]="George";
  student[1]="Jane";
  student[2]="Elroy";
  student[3]="Astro";
  student[4]="Judy";
This creates five elements in the array and sets their values. Pretty simple, eh? You can do a lot with arrays. We'll get back to them when we talk about objects.

Expressions


So.. Now that you have all these great variables, let's do stuff with them! But as with anything, we'll have to start from the beginning.

Operations

Here's a quick review of the operators that you probably know (from previous programming experience, looking else where in this manual, and/or knowing math.) Oh forget it! Basically if you don't know what "=", "+", "-", "*", and "/" mean, Javascript is above your head. Otherwise let's go on to the important operators that aren't so obvious.

(Condition) ? Variable1 : Variable2 Basically if (Condition) is true, the expression
has the value of variable1, otherwise it's variable2.
Variable1 += Variable2 Same as: Variable1 = Variable1 + Variable2
Variable2 -= Variable2 Same as: Variable1 = Variable1 - Variable2
Variable1 *= Variable2 Same as: Variable1 = Variable1 * Variable2
Variable1 /= Variable2 Same as: Variable1 = Variable1 / Variable2
Variable++ Same as: Variable = Variable + 1
Variable-- Same as: Variable = Variable - 1

Conditional Stuff

Variable1 == Variable2 Variable1 is the same as Variable2
Variable1 > Variable2 Variable1 is greater than Variable2
Variable1 >= Variable2 Variable1 is greater than or equal to Variable2
Variable1 < Variable2 Variable1 is less than Variable2
Variable1 <= Variable2 Variable1 is less than or equal to Variable2
Variable1 != Variable2 Variable1 is not equal to Variable2

One quick note on String operators. If you want to merge two strings, just add them in order as if they were numbers. (When you add strings, you get the sum(concatination) of the strings). Example:

  "This is a " + " string in " + " multiple parts."


Return to Front Page
Continue to next section: Functions