Home

Variables

 

Overview of Variables

 

Introduction

A typical program uses various and different values. Some are used for display to the user. Some are requested from the user. To manage these various values, the compiler reserves some sections of the computer memory to store them. Because the compiler cannot predict the types of values that a program would need, it leaves it up to the programmer to decide.

One of the instructions you will give to the compiler when creating a program is to specify how much memory space a certain value would need. This type of instruction is formulated as a declaration.

Variable Declaration

To declare a variable, type var, followed by a name for the variable, followed by a colon, and followed by a word that defines the type of value that would be stored in this memory space. The formula used is:

var VariableName : DataType;

This means that, to declare a variable, you must provide a name and specify the type of information the variable will carry.

Identifiers

An identifier is a word that is used to identify an entity in a program. There are rules you must follow when naming your variables. They are:

  • The first character must be a letter (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, or Z) or an underscore (_). Examples are _pages, country, or sound
  • After the first character, the name can contain letters, underscores, or digits (0, 1, 2, 3, 4, 5, 6, 7, 8, or 9). Examples are number1, _page6, complete_sound
  • The complete name must not be one of the words reserved for the compilers own use

JScript is case-sensitive. This means that case, CASE, and Case are three different words. Based on these rules, avoid using the following reserved words to name your variables:

abstract  boolean break byte case catch
char class const continue debugger decimal
default delete do double else enum
export extends false final finally float
for function get if implements import
in instanceof int interface internal long
new null package  private  protected public
return sbyte  set  short static  
super switch this throw true try
typeof uint ulong ushort  var void 
while with        

Besides these official keywords, avoid the following keywords when naming your variable because these words would make your code difficult to read or troubleshoot:

Array Boolean Double Number Object String
           

 

Data Types

 

Introduction

A data type is a word that indicates the amount of memory needed to store the value of a variable. As there are various types of values used in a program, there are also different data types. Some data types are meant to hold numbers. Some others are form particular values such as one being true or false. There are other data types designed to handle almost any type of data, also called an object. One of the rules you should make for yourself is to always use the right data type for a variable. This makes your code easier to read and maintain.

 

Variable Initialization

After declaring a variable, the compiler reserves an area of the computer memory for it. In a JScript .NET application, each declared variable receives a value, also called a default value. When a variable is declared receives a value, the variable is said to having been initialized. You also, when declaring a value, can give it an initial value of your choice. The value that a variable receives depends on its data type.

Characters

A character can be one of the following symbols: a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, and Z. Besides these readable characters, the following symbols are called digits and they are used to represent numbers: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. In addition, some symbols on the (US QWERTY) keyboard are also called characters or symbols. They are ` ~ ! @ # $ % ^ & * ( ) - _ = + [ { ] } \ | ; : ' < ? . / , > "

Besides the English language, other languages use other or additional characters that represent verbal or written expressions.

To declare a variable whose value would be a character, use the char keyword. Here is an example:

char Gender;

To initialize a character variable, include its value in single quotes. Here is an example:

var Gender : char = 'M';
 

Integers

An integer is a natural number defined as follows:

  • The first or most left character can optionally be a + or - sign
  • Whether the + or - character is omitted or not, the actually characters that specify the number are digits (0, 1, 2, 3, 4, 5, 6, 7, 8, or 9) only, in any combination
  • Between the digits that compose the number, there cannot be any letter or other special characters

Examples of natural numbers are 25, -4284, or 64485663. The .NET Framework provides various types of natural numbers.

An integer can be initialized with a decimal or a hexadecimal value. A hexadecimal is a number that starts with 0x or 0X and includes an combination of digits (0, 1, 2, 3, 4, 5, 6, 7, 8, or 9) and a, b, c, d, e, f, A, B, C, D, E, or F.

Byte: A byte is a small natural positive number that ranges from 0 to 255. To declare a variable for a small number, use the byte keyword.

Signed Byte: To declare a variable that can carry small numbers from -128 to 127, use the sbyte data type.

Short: A short integer is a natural number larger than the byte. To declare a variable that can hold natural numbers in the range of -32768 to 32767, you can use the short data type. Alternative, the .NET Framework provides the Int16 data type used for the same purpose.

Unsigned Short: To declare a variable that would carry positive natural numbers that range from 0 to 65535, you can use the ushort data type.

Integer: To declare a variable that can hold natural numbers, you can use the int data type. Here is an example:

var Natural: int;

Such a variable is initialized with a 0 value. This variable can hold values that range from -2147483648 to 2147483647.

Unsigned Integer: If you want a variable that can handle positive number from 0 to 4294967295, declare it using the uint data type.

Long: A natural number is referred to as long when its range is very large, from -1019 to 1019. To declare a variable for such a range of numbers, use the long data type.

Unsigned Long: To declare a positive long integer that can handle numbers from 0 to 1020, use the ulong data type.

 

 

Floating-Point Numbers

A number is referred to as floating when it can include a decimal portion, specified by a special character that depends on the language. In US English, this character is the period. Examples of floating numbers are 24.33 or 2684.6862

A floating number can also be represented in a scientific format. In this case, it uses the letter e or E followed by a sign as - or +, followed by a natural number called an exponent.

Single: A floating number is referred to as Single if it is meant to handle small numbers.

double: A double-precision number is one that can handle very large values from -10308 to 10308 with an accuracy of 15 digits. To declare a variable that can hold numbers in that range, you can use the Number or double data types.

decimal: To declare a decimal variable that can perform more precision than the double or number data type would allow, use the decimal data type.

 

Boolean Variables

A variable is referred to as Boolean if it can hold only one of two values: true or false. To declare a Boolean variable, use the boolean or the Boolean data type. Here is an example:

var IsReady : Boolean;

Once the variable has been declared, you can use it as you see fit. For example, you can pass it to print() to display its value to the user. Here is an example:

var IsReady : Boolean;

print(IsReady);

When you declare a Boolean variable, the compiler initializes it with a false value. At any time, you can either initialize it with, or change its value to, true or false.

Strings

A string is an empty space, a character, a word, or a group of words that you want the compiler to consider "as is".
Primarily, the value of a string starts with a double quote and ends with a double-quote. An example of a string is "Welcome to the World of JScript .NET Programming!". You can include such a string in print method to display it on the console. Here is an example:

print("Welcome to the World of JScript .NET Programming!");

This would produce:

Welcome to the World of JScript .NET Programming!

Sometimes, you will need to use a string whose value is not known in advance. Therefore, you can first declare a string variable. To do this, use the String keyword (in fact, it is a class) followed by a name for the variable. The name will follow the rules we defined above. An example of a string declaration is:

var Welcome : String;

After declaring a string, you can give it a primary value by assigning it an empty space, a character, a symbol, a word, or a group of words. The value given to a string must be included in double-quotes. After initializing a string variable, you can use its name in any valid operation or expression. For example, you can display its value on the console using print. Here is an example:

var Welcome : String;

Welcome = "Welcome to the World of JScript .NET Programming!";
print(Welcome);

 

 

Previous Copyright © 2004 FunctionX, Inc. Next