COM271, Week 7

Javascript Overview: Variables

Syllabus | Table of Pages | Assignments | References and Useful Links

The basic data types in JavaScript are numbers, strings (i.e. text), and Booleans (True or False); the language also supports objects, arrays, and functions.

Variables: Variables hold data. They have 4 qualities:

  1. identifier (name)
  2. value
  3. type
  4. place in memory

Variables should (i.e., it is good programming practice) be declared before using: var myVariableName;

This would name the variable and set aside a place in memory for it, without setting a value or a type. If an undeclared variable name appears on the left side of an expression, a JavaScript interpreter will implicitly declare the variable, setting aside a place in memory, etc.

Types

:

An object contains both data and code. An object's data members are its properties. Its code, including functions and other objects, are its methods. Properties are accessed using a .propertyname syntax. Methods use a .methodname() syntax. If there are arguments passed to the method, they are included within the parentheses. Built-in objects include Window and Document to provide access to windows and the XHTML document itself (e.g., to open a window and modify a page of text), and objects to process data (e.g., Date, Math, RegExp). User created objects use var myObjectName = new Object() to instantiate a new instance of an object. An Array is a composite object, an ordered list that contains both primitive and complex data types. Functions are objects that contain executable code; they may be passed lists of 1 or more arguments, separated by commas. The typeof operator returns a string indicating the type of a variable.

Weak Typing: JavaScript variables have types, but they are not determined when the variable is declared, and they can change if a different type of variable is assigned.

Type Conversions. A variable's type will change if a different type data is stored in the variable, automatically. A variable's type can also be changed using methods (e.g., toString( ) or toInt( ). The strict equality operator (===, that's right: 3 equal signs!) is a Boolean that evaluates to true only if its two operands are equal and of the same type.

Naming variables

Good JavaScript coding means choosing good names for variables, making code easier to understand. Variable identifiers (=name) can contain any characters, except that they must not begin with a digit and the identifier can not be the same as any reserved word in JavaScript (e.g., string, Object, return, etc.). Identifiers should give some strong clue as to their meaning; an array of week day names, for example, could be ArrDayNames. Variable names can use the camelback style or underscores to make them easier to read, e.g., strFirstName. Short variable names may save slightly on download times, but they should first be clear as to meaning. A consistent naming convention (e.g., begin string identifiers with str..., integers with int..., etc.) also helps improve code readability. Remember that JavaScript is case-sensitive, and this includes variables names. That is, x and X are different identifiers.

Declaration: Variables may be declared explicitly (always preferred), as var x;, and assigned values, as var pi = 3.14159. If not declared explicitly, any undeclared variable will be automatically declared upon first use (always a discouraged practice).

Scope: A variable declared in JavaScript in the main body of the page is global, and may be used anywhere on the page or in any function. A variable declared within a function is local and is not visible or available for use outside of the function. Similarly, any variable declared within an event handler is local. If a variable of the same name is re-declared within a function or event handler, it temporarily takes on new values, i.e., it replaces the values of the global variable within the local context only, but does not alter the value of the global variable outside of the function or event handler.