0% found this document useful (0 votes)
45 views

19 CSS Exp2

The document describes experiments with JavaScript array methods. It includes code snippets to: 1) Create an array, find its length, display the original array, pop one element and display the updated array. 2) Sort an array in ascending and descending order using the sort method. 3) Demonstrate the push, pop, reverse, join, and concat array methods on sample arrays.

Uploaded by

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

19 CSS Exp2

The document describes experiments with JavaScript array methods. It includes code snippets to: 1) Create an array, find its length, display the original array, pop one element and display the updated array. 2) Sort an array in ascending and descending order using the sort method. 3) Demonstrate the push, pop, reverse, join, and concat array methods on sample arrays.

Uploaded by

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

Experiment No 02.

• Aim: Develop a JavaScript to implement array functionality.


• Practical Questions:
1. Create an Array, find its length, display the original array,
pop one element from the array and display current array.
CODE
<html>
<h3>Array Pop</h3>
<body>
<script type = "text/javaScript">
var array = new Array();
for(i=0;i<10;i++){
array[i] = i+1;
}
document.write("Initial Array: <br>"+array+"<br>");
document.write("Length of Array="+array.length+"<br>");
document.write("<br>Array length after Popping
"+array.pop()+"<br>"+array+"<br>");
document.write("Lenght of Array="+array.length);
document.write("By 19-Soham Parab")
</script>
</body>
</html>
OUTPUT

CSS CO5I(B) 19
2. Array sort Method.
CODE
<html>
<h2>Array Sort Methods</h2>
<body>
<script type = "text/javaScript">
var array = new Array();
array = [8,5,9,4,7,6,2,3,1];
document.write("Initial Array:<br>"+array+"<br>");
document.write("<br>Array Sorted in Ascending
order:<br>"+array.sort()+"<br>");
array.sort(function c(a,b){
return b-a;
})
document.write("<br>Array Sorted in Descending order:<br>"+array);
document.write("By 19-Soham Parab")
</script>
</body>
</html>
OUTPUT

CSS CO5I(B) 19
3. Array push, pop, reverse, join, concat method.
CODE
<html>
<h3>Array Operations</h3>
<body>
<script type = text/javaScript>
var array = new Array("AJP","CPP","EST");
document.write("Initial Array: "+array+"<br><br>");

var poppedElement = array.pop();


document.write("Array after popping:
"+poppedElement+"<br>"+array+"<br><br>");

array.push(poppedElement);
document.write("Array after pushing:
"+poppedElement+"<br>"+array+"<br><br>");

document.write("Array after Reversing:<br>"+array.reverse()+"<br><br>");

var array2 = new Array("CSS","OSY");


var concatedArray = array2.concat(array)
document.write("Array after Concating:<br>"+concatedArray+"<br><br>");

document.write("Array after Joining: <br>"+concatedArray.join(" |


")+"<br><br>");
document.write("By 19-Soham Parab")

</script>
</body>
</html>

CSS CO5I(B) 19
OUTPUT

CSS CO5I(B) 19
• Exercise
1. Define Array.
Ans: An array is a collection of elements of the same type placed in
contiguous memory locations that can be individually referenced by
using an index to a unique identifier.

2. How to declare array in JavaScript?


Ans: In JavaScript there are three ways to declare an array:
1.Array Literal:
In this method, square brackets “[ ]” are used for declaring an array
with the literals passed in square brackets as elements.
Syntax: var arrayName = [element1,element2,……..];
2.Array Instance:
In this method, we create instance of array class without adding
elements to it. Later, we can add elements in the array using
arrayName[index] = element;
Syntax: var arrayName = new Array();
3.Array Constructor:
In this method, we create object of the array class and use it’s
constructor to pass elements as parameters.
Syntax: var arrayName = new Array(element1,element2,………..);

3. Explain the use of Join Function.


Ans: The join function is used to join i.e. combine the elements in an array
to a String. While joining it adds default separator ‘ , ’ to the string after
each element. If we want to add any different separator then we can pass
it with the function as a parameter.
For e.g.: default separator: arrayName.join();
Passing separator: arrayName.join(*);

CSS CO5I(B) 19

You might also like