MIC102G » Lecture 2

Update: January 8th, 2009

Values

A value is a piece of data. Values are stored in variables.

Variables

Every programming language needs a way to store data. Whether it's a person's name or a counter to indicate how many pages have been visited, it has to be stored somewhere in memory.

Variables are placeholders in programming languages for data which may or may not be changed during the execution of a program. They hold one value at a time. When a new value is stored in a variable, the old value is gone. If a new variable is declared without assigning it a value, it will have the value of null. Another way to say this is that the variable was not initialized.

This is done by first creating a unique identifier for the data, which is usually a set of alphanumeric characters like a name, with no spaces.

Assignment to the variable is done using the equals sign (=).

On the right of the equals sign, you put your data.

Examples:

myAge = 29

myName = 'Adam'

myFunction = function() {  /* function code to execute goes in here */ };

Data Types

The type of data inside of a variable determines the variables data type.

Most languages have a set of useful built in data types, these are essentially objects that are defined to have certain properties, methods (functions) and storage limits.

Here are some data types that you will find in most languages:

  • Number
  • A numeric value that can be used in a calculation.

  • String
  • Text that is enclosed within quotation marks. The text may contain numbers and any other character besides those which make up an alphabet. Numbers in strings are treated as text.

  • Boolean
  • Either true or false. These values are used in situations of decision making. (The term Boolean is named after the 19th century mathematician George Boole [1815 - 1864], who devised a mathematical means to represent logical thought. This valuable insight, in turn, has later led to innovations like telephonic switching circuits and the modern computer.)

  • Null
  • The absence of a value, or nothing. It provides a means to test whether a variable has a value or not.

  • Object
  • A reference to the location in memory where the object is stored.

  • Function/method
  • A reference to the location in memory where the function is stored and can be run. A method is another name for a function commonly associated with functions that are properties of objects or classes. If I have an object that represents a person, a method of that person might be to speak.

Initialization

Most languages require that variables are initialized at first use. This allows the system to know that it needs to set aside a certain amount of memory space for that variable. The scope of a variable is usually determined by where in the code a variable is initialized.

Here is an example of initializing a variable in the JavaScript language:

var myName

The declaration has two parts. The var is a keyword in JavaScript for the browser to set aside space in memory for a value, that is, create a variable. The second part is the name of the variable. This is how the programmer can refer to the space in memory set aside. Whenever that name is used in a program and not redeclared, the value stored in memory will be used. The term identifier is used when describing the second part of a variable declaration.

Identifiers have some restrictions. In JavaScript, Identifiers must begin with a letter or an underscore ('_'). The rest of the characters that make up an identifier must be from the alphabet, a number, or the underscore. Capital letters may be used. Often, identifiers are made in "camel-case", where multiple words are combined to form the identifier and the beginning letter of each of the interior words is capitalized, not the first word; for example: numberFive. The case of a letter in an identifier matters: numberFive is different from numberfive - they refer to two different variables.

Another restriction on identifiers has to do with the syntax of the JavaScript language. There are several words - keywords like var and null, and reserved words like try and catch - which have specific meanings in the language. To attempt a variable declaration such as:

var var

will cause confusion and lead to problems. The same goes for the names of objects like window and document. Likewise for names of functions that are intrinsic to the language, such as alert, open and prompt. It is best to avoid using any such words as identifiers. One workaround would be to add "my" to the front and camel-case it; for example, myVar. The following two tables list keywords and reserved words to avoid as identifiers.

JavaScript

The script tag:

External: <script type="text/javascript" src="/path/to/file.js"></script>

Embedded:

<script><!--

alert(1);

--></script>

Examples in Class

No comments yet.
You must be logged in to post a comment.
TOP