An array is a group of memory locations that all have the
same name and normally are of the same type .
To refer to a particular location or element in the array, we
specify the name of the array and the position number of the
particular element in the array.
The first element in every array is the zeroth element.
Thus, the first element of array c is referred to as c[0], the
second element as c[1] and so on.
The position number in square brackets is called an
index and must be an integer .
The array’s length can be found by using the array’s
length property, as in:
c.length
Arrays occupy space in memory.
Actually, an array in JavaScript is an Array object.
Use the new operator to create an array and to specify the
number of elements in an array.
var c = new Array( 12 );
It uses operator new to allocate an array of five
elements and an empty array.
The script demonstrates initializing an array of existing
elements and also shows that an array can grow
dynamically to accommodate new elements.
To link a javascript code, use the script element’s src
attribute to specify the location of the JavaScript
file (named with the .js filename extension)
Array.html
<head>
<script src = "InitArray.js"></script>
</head>
<body>
</body>
InitArray.js
function start()
{
var n1 = new Array( 5 ); // allocate five-element array
var n2 = new Array(); // allocate empty array
var length =n1.length;
for ( var i = 0; i < length; ++i )
{
n1[i] =i;
}
for ( i = 0; i < 5; ++i )
{
n2[ i ] = i;
}
document.write(”<table><tr><th>Index</th><th>Value</th>
“);
for ( var i = 0; i < length; i++ )
{
document.write("<tr><td>" + i + "</td><td>" + n1[ i ]
+"</td></tr>“);
}
document.write(“</table>“);
}
Using an Initializer List
If an array’s element values are known in advance, the
elements can be allocated and initialized in the declaration of
the array. There are two ways in which the initial values canbe
specified. The statement
var n = [ 10, 20, 30, 40, 50 ];
The statement
var n = new Array( 10, 20, 30, 40, 50 );
also creates a five-element array with indices of 0, 1, 2,
3 and 4.
Input.html
<html>
<head>
<script src = "SumArray.js"></script>
</head>
<body>
</body>
</html>
HTML5 document that displays the sum of an array's
elements
SumArray.js
function start()
{
var abc = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];
var total1 = 0, total2 = 0;
var length = abc.length;
for ( var i = 0; i < length; ++i )
{
total1 =total1+ abc[ i ];
}
document.write(total1);
for ( var j in abc )
{
total2 =total2+ abc[ j];
}
document.write(total2);
}
The for…in Repetition Statement
In this example, we introduce JavaScript’s for…in statement,
which enables a script toperform a task for each element in an
array .
When you use for…in, JavaScript automatically determines
the number of elements in the array.
As the JavaScript interpreter iterates over theArray’s elements,
variable element is assigned a value that can be used as an
index for theArray.
In the case of an array, the value assigned is an index in the
range from 0 up to, but not including, theArray.length
Two ways to pass arguments to functions (or methods)
in many programming languagesare pass-by-value
and pass-by-reference.
When an argument is passed to a function by value,
a copy of the argument’s value is made and is passed to
the called function.
In Java-Script, numbers, boolean values and strings are
passed to functions by value.
With pass-by-reference, the caller gives the called
function access to the caller’s data and allows the
called function to modify the data if it so chooses.
This procedure is accomplished by passing to the
called function the address in memory where the
data resides.
Pass-by-reference can improve performance because it
can eliminate the overhead of copying large amounts of
data, but it can weaken security because the called
function can access the caller’s data.
To pass an array argument to a function, specify the
array’s name (a reference to the array) without
brackets. For example, if array hourlyTemperatures has
been declared as
var hourlyTemperatures = new Array( 24 );
then the function call
modifyArray( hourlyTemperatures );
The function header is
function modifyArray( b )
Input.html
<html>
<head>
<script src = “modify.js"></script>
</head>
<body>
</body>
</html>
modify.js
function start()
{
var a=[1,2,3,4];
modifyArray(a);
}
function modifyArray (thearray)
{
for(var j in thearray)
{
thearray[j]=thearray[j]*2;
}
}
document.write(thearray);
By default, the sort() function sorts values as strings.
(e.g):
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();
The output is : Apple, Banana,Mango,Orange
However, if numbers are sorted as strings, "25" is
bigger than "100", because "2" is bigger than "1".
So use a compare function for sorting numerical
values.
//sort.js
function start()
{
var a = [ 10, 1, 9, 2, 8, 3, 7, 4, 6, 5 ];
a.sort(compareIntegers);
}
function compareIntegers(value1,value2)
{
return parseInt(value1)-parseInt(value2);
}
When working with data stored in arrays, it’s often
necessary to determine whether an array
contains a value that matches a certain key value.
The process of locating a particular element value in
an array is called searching.
The Array object in JavaScript has built-in methods
indexOf and lastIndexOf for searching arrays.
Method indexOf searches for the first occurrence of
the specified key value, and method lastIndexOf
searches for the last occurrence of the specified key
value.
If the key value is found in the array, each method
returns the index of that value; otherwise, -1 is returned
var a = [1,2,3,4];
var search =3;
var element = a.indexOf( search );
document.write(“Index of 3 in the array”+element);
Multidimensional arrays with two indices are often
used to represent tables of values consisting of
information arranged in rows and columns.
Arrays that require two indices to identify a particular
element are called two-dimensional arrays.
Multidimensional arrays can have more than two
dimensions. JavaScript does not support
multidimensional arrays directly, but it does allow you
to specify arrays whose elements are also arrays, thus
achieving the same effect.
var array1 = [ [ 1, 2, 3 ], // row 0
[ 4, 5, 6 ] ]; // row 1
var array2 = [ [ 1, 2 ], // row 0
[ 3 ], // row 1
[ 4, 5, 6 ] ]; // row 2
Creating Two-Dimensional Arrays with new
A multidimensional array in which each row has a different number of
columns can be allocated dynamically, as follows:
var b;
b = new Array( 2 ); // allocate two rows
b[ 0 ] = new Array( 5 ); // allocate 5 columns for row 0
b[ 1 ] = new Array( 3 ); // allocate 3 columns for row 1
Initialising Multidimensional arrays
var array1 = [ [ 1, 2, 3 ], [ 4, 5,6 ] ];
for ( var row in array1 ) //takes the first dimension of the array1
{
for (var col in array1[row])
{
document.write (“The elements of the array”
+array1[row][col]);
}
}
Using for loop finding sum of elements in the array
var a= [[1,2,3] ,[5,6,7]];
var total = 0;
var rows = a.length; // returns the number of rows in array
for ( var row = 0; row < rows; ++row )
{
var columns = a[ row ].length;
for ( var col = 0; col < columns; ++col )
{
total =total+ a[ row ][ col ];
}
}
Using for.. in loop
var a= [[1,2,3] ,[5,6,7]];
var total = 0;
for (var row in a ) // number of rows
{
for ( var col in a[ row ] ) // number of columns
{
total = total +a[ row ][ col ];
}
}