Variable Conventions


When dealing with information two types of variable values occur. The first is numbers, numeric, and the various ways that they may be written, and the second is typewriter characters, otherwise known as alphanumeric. There is a forbidden typewriter character and this is "$". It should never be used in JS since browsers cannot handle it (no pun intended). A convention for each type of value used in JS must be followed, and a convention must be followed for how they may be named.

A variable can be named just about anything. However, three rules have to be obeyed:

  • The first character in the name must be an
    alphabetic character or an underscore, e.g.,

       var npeople;
       var table();
       var _top;

  • After this any other characters (but a $) can
    be used whether alphabetic, numeric,
    or an underscore, e.g.,

       var _loc10;
       var cost(2, 5);
       var face2face;

  • JS variable names are case sensitive. The
    use of capital letters distinguishes a
    variable from its non-capitalized
    counterpart, e.g.,

       var god;        is different from
       var God;

  • JS reserved words cannot be used


Alphanumeric variables have their values assigned by enclosing them in quotation marks. They are then known as string variables. A string variable is not restricted to alphanumerics. Any number, if enclosed in quotation marks is a string variable. Thus, these are both string variables:

var today="Wednesday";

var phone="0915551234";


Certain JS reserved words can be used to define a variable, though they cannot be used as names for one, e.g, "true" and "false". As will be discussed later, these are used to set conditions that either are or are not. These are not enclosed in quotes:

var answer=true;

var checkd=false;


When assigning numbers, any name can be used. It does not matter whether a number is an integer (a whole number) or contains a decimal (a floating point number). Practically, for some, it is a good idea to restrict the names of whole numbers to those which begin with I, J, K, L, M, or N, whether capitalized or not. This helps in later interpreting what has been done in a particular coding. The most usual types of numbers encountered are expressed as follows:

Integervar index=15;
Floating-pointvar temp=98.62;
Exponent (power of 10)var AvagadroN=6.023e+23;


Numbers may be either + or -, as may be an exponential value. Exponentiation can use either "e" or "E".



<< PreviousNext >>