You are on page 1of 7

UNIT III

Arrays
Group of variables referred by single name which are similar type is
called an array
We can locate a particular element of an array using array name and its
position number. The array positions are starts from o to size-1 of the
array
An array in java script is an Array object..we have to use new operator
to allocate the memory for an array dynamically.
Syntax
var arrayname=new Array(size);
Ex:
var a=new Array(5); // int a[5];
Here the individual elements of an array are a[0],a[1],a[2],a[3],a[4]

Initializing an array
Var a=new Array(10,20,30,40,50);
Var a=[10,20,30,40,50];
The length of the array can be determined by using length property
Ex: a.length; -> 5
Storing and displaying values from an array
<html><head>
<script language="javascript">
1
var a=new Array(5);
for(i=0;i<5;i++)
a[i]=parseInt(prompt("enter a number"));
for(i=0;i<5;i++)
document.writeln(a[i] + " ");
</script></head></html>
//Max from array
<html>
<head>
<script language="javascript">
var a=new Array(5);
for(i=0;i<5;i++)
a[i]=parseInt(prompt("enter a number"));
max=a[0];
for(i=0;i<a.length;i++)
{
if(a[i]>max)
max=a[i];
}
document.writeln("max=" +max);
</script>

2
</head>
</html>
Programs
// store the values in to array and display in reverse order
// store the values in to array and display that with their square and cube
// store the values in to array and find out the minimum value

Multidimensional arrays
Multidimensional arrays are not directly provided in JavaScript. If we
want to use anything which acts as a multidimensional array then we
need to create a multidimensional array by using another one-
dimensional array.
So multidimensional arrays in JavaScript is known as arrays inside
another array.
They are useful for organizing data in a structured manner, allowing
for tabular or matrix-like storage
The multidimensional array can be created as follows
Var arrayname=new Array(3); // allocation for 3 rows
a[0]=new Array(3); //allocating 3 cols for first row
a[1]=new Array(3); //allocating 3 cols for second row

a[2]=new Array(3); //allocating 3 cols for third row


the above declaration declared 3X3 9 variables

3
they are
a[0][0] a[0][1] a[0][2]
a[1][0] a[1][1] a[1][2]
a[2][0] a[2][1] a[2][2]
Initializing the multi dimensional array
var a=[[1,2,3],[4,5,6],[7,8,9]];
// program for accepting values in to a multidimensional array and
display that
<html>
<head>
<script language="javascript">
a=new Array(3);
a[0]=new Array(3);
a[1]=new Array(3);
a[2]=new Array(3);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
a[i][j]=parseInt(prompt("enter a number for a[" +i + "]["+j+"]"));

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
4
document.writeln(a[i][j] +" ");

document.writeln("<br>");
}
</script>
</head>
</html>
// program for addition of two matrices
// multiplication of two matrices

References and reference parameters


There are two types of parameter passing techniques. They are
1. Call by value
2. Call by reference

When the arguments are passed to functions by using call by value a copy
of argument value is passed to called function. The changes to the called
function copy do not affect the original values in calling function. In the
java script numbers, Boolean values are passed to functions by using call
by value technique.
When arguments are passed to functions by using call by reference the
caller gives the called functions to directly access the callers data and
modify that.

5
Call by reference can improve the performance because it eliminates the
overhead of copying large amount of data but it is weaker in the security
issue because called function can access the calling data.
In java script all the objects and arrays are passed to functions by using
call by reference.

Passing arrays to functions


// program to passing array as parameter to function
(call by reference)
<html> <head> <script language="javascript">
var a=new Array(5);
for(i=0;i<a.length;i++)
a[i]=parseInt(prompt("enter a number"));
disp(a);
modi(a);
document.writeln("after modifying array values are");
disp(a);
function modi(x)
{
for(i=0;i<x.length;i++)
x[i]=x[i]+1;
}

6
function disp(x)
{
for(i=0;i<x.length;i++)
document.writeln(x[i] +" ");
}
</script></head></html>

You might also like