0% found this document useful (0 votes)
9 views5 pages

Exp 3

Uploaded by

Arya M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

Exp 3

Uploaded by

Arya M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Experiment No.

3
Aim: Develop JavaScript to implement Array functionalities.
Theory:
Array:
It is a collection of more than one element value of similar data type. Array is used store multiple values
required for processing data. An array has user defined name long with index value to identify each element
stores in an array. A combination of array name and an index value is same as having a single variable.

Syntax to declare an array variable:


var array_name=new Array( );
- var is a keyword used to declare a variable
- array_name can be any combination of characters that gives identity to an array.
- new is a keyword that allocates memory to array elements.
- Array ( ) is used to create an array variable. Values to be stored inside array can be mentioned inside
the round brackets.
Syntax to initialize an array:
var array_name=new Array(‘value1’,’value2’, … , ‘value n’);

length property of an array:


It is used to determine number of elements stored in an array. It determines actual number of elements
present in an array not the size of an array.
Syntax: var variable_name=arrayname.length;
Example: var len=products.length;

Looping an array:
An array element can be accessed using an index variable. By using loop, we can traverse through all the
elements of an array by incrementing index variable by one, after every visit to an element.
In looping, a index variable/counter variable is initially assigned with 0 position. By using for loop or while
loop, each element from an array can be visited with index variable. Initially index variable will point to first
element stored at 0th position. Then index variable is incremented by one to point to next position and so on.
The loop will execute till all elements are visited and index variable points to length value.

Sorting An array:
Arranging all array elements in alphabetical order for strings and ascending order for numbers.
sort( ) function is used to perform sorting on array elements.
Syntax: array_name.sort( );

Adding element in an array:


push( ) method: It is used to add a new element at the end of an array. It allocates memory to new element
in an array by incrementing index position by one.

Syntax: array_name.push(“value”);
Combining array elements into a string:
1. concat( ) : It is used to merge all elements of an array into one string.
Syntax: var variable_name=arrayname.concat( );
Above syntax combines elements into one string and separate each element with a comma.

2. join( ) : It is used to merge all elements of an array into one string.


Syntax: var variable_name=arrayname.join(‘char’);
A join method uses comma to separate array elements in a string but we can replace comma with any other
character also. ‘char’ in the syntax indicates character to be replaced with comma.

Changing elements in an array:


1. Reversing array elements:
reverse( ) : It is used to reverse the order of elements of an array.
Syntax: array_name.reverse( ) ;

2. Removing an element from an array:


shift( ) : It is used to remove first element from an array and return it. It also moves second element at the
top of an array.
Syntax: var variable_name=array_name.shift( );
pop( ) : It is used to return and remove the last element of an array.
Syntax: var variable_name=array_name.pop( ) ;

Programs:
Develop a JavaScript to declare an array with five elements and display all on a webpage.

<html>
<head></head>
<body>
<script type="text/javascript">
var arr=new Array ("book", "pen", "pencil", "eraser",
"notebook");
var i;
document. write ("Array Elements are: ");
for (i=0; i<arr.length;i++)
{
document. write("<br>" + arr[i]);
}
</script>
</body>
</html>
Develop a JavaScript code to declare an array with five names of flowers and display them in sorted
order.

<html>
<head></head>
<body>
<script type="text/javascript">
var flowers=new Array("Rose", "Jasmine", "Mogra",
"Lotus","Jai");
var i;
document.write("Array Elements are : ");
for(i=0;i<flowers.length;i++)
{
document.write("<br>" + flowers[i]);
}
flowers.sort();
document.write("<br>Sorted Elements are : ");
for(i=0;i<flowers.length;i++)
{
document.write("<br>" + flowers[i]);
}
</script>
</body>
</html>

Develop a JavaScript code to declare an array with five elements and display them in reverse order of
entry in an array.

<html>
<head></head>
<body>
<script type="text/javascript">
var flowers=new Array ("Rose", "Jasmine", "Mogra",
"Lotus", "Jai");
var i;
document. write ("Array Elements are: ");
for (i=0; i<flowers. length; i++)
{
document. write("<br>" + flowers[i]);
}
flowers.reverse();
document. write("<br><br>Array Elements in
reverse order are: ");
for (i=0; i<flowers. length; i++)
{
document. write("<br>" + flowers[i]);
}
</script>
</body>
</html>
Develop a JavaScript code to declare an array with three colors. Merge all array elements in one
string, separated with ‘,’ character and display the string.

<html>
<head></head>
<body>
<script type="text/javascript">
var arr=new Array("pink","blue","red");
var i;
document. write("Array elements are : ");
for(i=0;i<arr.length;i++)
{
document. write("<br>" + arr[i]);
}
var str=arr.concat();
document. write("<br>String = " + str);
</script>
</body>
</html>

Develop a JavaScript code to declare an array with three colors. Merge all array elements in one
string, separated with ‘&’ character and display the string.

<html>
<head></head>
<body>
<script type="text/javascript">
var arr=new Array("pink","blue","red");
var i;
document. write ("Array elements are: ");
for(i=0;i<arr.length;i++)
{
document. write("<br>" + arr[i]);
}
var str=arr.join('&');
document. write("<br>String = " + str);
</script>
</body>
</html>

Develop a JavaScript code to declare an array with three elements and Shift all elements from one
array to another array.

<html>
<body>
<script type="text/javascript">
var a=new Array("pen","pencil","book");
var b=new Array ();
var i;
document. write ("Elements of array a= ");
for (i=0; i<a.length;i++)
document. write("<br>" + a[i]);
for(i=a.length;i>0;i--)
{
b.push(a.shift());
}
document. write ("<br><br>Elements of array b= ");
for (i=0; i<b.length; i++)
document. write("<br>" + b[i]);
</script>
</body>
</html>

Practical Questions:
1. Write a JavaScript program to arrange all elements in descending order.
2. Write a JavaScript program to accept five numbers in an array and display them in sorted order.
3. Write a JavaScript program to copy one array into another array.

Conclusion:
With all the concepts based on array functionality, successfully developed and executed JavaScript codes
with correct output.

You might also like