Arrays in JScript

While variables are used for storing single values (a string, a number, etc.), arrays special variables that can hold for multiple values in the same variable. It's pretty difficult to store 50 names of different people in 50 variables, so this is the main purpose of a variable: it allows you to store as many elements as you want in it. You've already used arrays in the loops created before, and they didn't puzzle you, because they are quite logical and easy to understand.

Because you have such many elements, there must be a very quick way to access them. Well, you should know that there is this thing called indexing, which means that each item has an associated index. JScript indexes an array's elements starting from zero and increments the element's index with each new addition, this way you can always find out the last index of an element: it's the total number of elements minus one.

A second way to index an array is by using strings. These types of array are often referred to as associative arrays, because they have a string associated with each element. This is really useful when you're using small arrays and you want to know exactly what each element is all about. The index, either numeric or string, is always enclosed by brackets "[]".

There are two types arrays in Jscript: typed arrays and object arrays. The typed array has a base data type, so this means that each elements of the array must be of the same type (for example, all elements are strings). You can declare a typed array using the "new" operator, for instance:

var country_capitals = new Array(4)

This creates a new array, with four elements. You can do the same, by specifying each element:

var country_capitals = new Array("Washington", "Paris", "London", "Moscow")

In order to refer to one of the elements of the array, you'll have to use the name of the array, and the index number. Remember that the first index number is 0. So, when you want to assign data to the elements of the array, you have to use the following syntax:

country_capitals[0] = "Washington"

country_capitals[1] = "Paris"

country_capitals[2] = "London"

country_capitals[3] = "Moscow"

When you need to, you can retrieve the data stored in the elements of the array, with this syntax:

town = country_capitals[0]

There is a wide variety of uses for the arrays and the data they store. In web pages, you will often see the elements of an array displayed as tables. Also, you can use them for a basic word search, or a filter operation.

Properties can be attributed to an array, by using the syntax: object.property_name. There are three properties for the arrays: prototype (which ads properties to the array), length (for the number of elements) and constructor (with the function that created the prototype of the object).

There are several methods you can use in connection with the arrays, through which they perform the respective actions. The syntax is object.method_name().

One such function you will often find is concat() which joins two arrays (or more, as the case may be). The returned result will be a new array, of course. Another method is pus("element"), which ads the respective element (or more than one) at the end of the given array. In this case, the returned value is the new length of the array.

The names of these methods are quite intuitive, so in many cases you'll understand what they do when you'll see them – reverse(), for instance, reverses the order of the elements of the array, while sort(), well, it sorts the elements. You should spend some time looking at these methods – you don't need to learn them by heart or anything, but it would be useful to know that they exist, so you may use them when you need them.

View Comments

  1. pattiefmartin March 10th, 2009 1:50 pm

    Thank you for sharing this information.
    Reverse Access Livedoor

  2. johnmrubio March 10th, 2009 1:51 pm

    I love this issue very much
    Reverse Access Livedoor

  3. johnmrubio March 10th, 2009 2:29 pm

    I love this issue very much
    Reverse Access Livedoor

  4. aaronasjones March 14th, 2009 10:18 am

    Thank you for sharing this information.
    Reverse Access Livedoor

Leave a reply