Practical No.3: Develop JavaScript to Array functionalities.
Array:
Arrays in JavaScript. In JavaScript, array is a single variable that is used to store different
elements. Unlike most languages where array is a reference to the multiple variable, in
JavaScript array is a single variable that stores multiple elements.
1. The Array object lets you store multiple values in a single variable.
2. It stores a fixed-size sequential collection of elements of the same type.
3. An array is used to store a collection of data, but it is often more useful an array as a
collection of variables of the same type.
Declaring an Array:
In JavaScript the array can be created using Array object.
Syntax:
var a = new Array(10);
In above syntax array is created of 10 elements.
Using new operator we can allocate the memory dynamically for the arrays.
We can also define array by following ways:
var fruits = new Array( "apple", "orange", "mango" );
Initializing an Array:
Initializing is the process of assigning a value when either a variable or array is
declared.
While initializing an array:
1. Declare name of the array
2. Make use of the keyword new
3. Each value in array as an array element & separated by comma.
Example: var mylist= new Array(20,40,50,60)
Access the Elements of an Array:
Array element can be access by referring to the index number.
Syntax: array[index]
Array functions:
1. Length: It will return the length of array.
Syntax: [Link]();
2. Add:By using length function, we can add element in array.
Syntax: array[[Link]];
3. Sort: The elements can be sorted using the built in function sort.
Syntax: [Link]();
4. Join: For combining array elements into string we can use join() function.
Syntax: [Link]();
5. Shift(): Shift method removes the first element from an array and returns that
element.
Syntax: [Link]();
6. Push(): Push method appends the given element(s) in the last of the array and
returns the length of the new array.
Syntax: [Link](element);
7. Pop(): Pop method removes the last element from an array and returns that element.
Syntax: [Link]();
8. Reverse(): Reverse method reverses the element of an array. The first array element
becomes the last and the last becomes the first.
Syntax: [Link]();
Conclusion: We understand the different array functionalities by executing
JavaScript’s.
Practical No.3: Develop JavaScript to Array functionalities.
<html>
<body>
<script>
[Link]("Array Functionalities :"+ "<br/>");
var a=[20,30,10,60,40];
[Link]("1. The Length of array is : " + [Link]);
[Link]("</br>");
[Link]("2. Displaying Array Elements:");
for (i=0; i<[Link]; i++){
[Link](a[i] + "<br/>");
}
[Link]("3. The elements is Added in array:" + "<br>");
a[[Link]]=70;
a[[Link]]=90;
[Link]("Display Array Elements:");
[Link]("</br>");
for (i=0; i<[Link]; i++){
[Link](a[i] + "<br/>");
}
[Link]();
[Link]("4. Display Array Elements After Sorting:");
[Link]("</br>");
for (i=0; i<[Link]; i++){
[Link](a[i] + "<br/>");
}
var num=[Link]();
[Link]("Removed element is : " + num + "<br>");
[Link]("5. Dispaly Array element After shift function is : " +
"<br>"); for (i=0;i<[Link];i++){
[Link](a[i] + "<br>");
}
var length = [Link](100);
[Link]("6. Dispaly Array element After push function is: " + a );
[Link]("</br>");
var element = [Link]();
[Link]("7. Dispaly Array element After pop function is: " + a );
[Link]("</br>");
var arr = [Link]();
[Link]("8. Dispaly Array element After Reverse function is: " + arr );
</script>
</body>
</html>
*********************************OUTPUT ************************************
Array Functionalities :
1. The Length of array is : 5
2. Displaying Array Elements:
20
30
10
60
40
3. The elements is Added in array:
Display Array Elements:
20
30
10
60
40
70
90
4. Display Array Elements After Sorting:
10
20
30
40
60
70
90
Removed element is : 10
5. Dispaly Array element After shift function is :
20
30
40
60
70
90
6. Dispaly Array element After push function is: 20,30,40,60,70,90,100
7. Dispaly Array element After pop function is: 20,30,40,60,70,90
8. Dispaly Array element After Reverse function is: 90,70,60,40,30,20