Professional Documents
Culture Documents
KADAM
--------------------------------------------------------------------------------------------------------------------------------------
UNIT:2 (14 Marks)
Array, Function and String
__________________________________________________
Array:
An Array is an object that can store a collection of items. Array is useful when
you need to store large amount of data.
You can access the items in an array by referring to its index numbers and index
of the first element of an array is zero.
An array in JavaScript can hold different elements. We can store numbers,
Strings, character and Boolean in a single value
<html>
<head>
<script language="javascript" type="text/javascript">
var arr1=["a","b","c"];
document.write(arr1);
</script>
</head>
<body>
</body>
</html>
Output:
a, b, c
1
CLIENT SIDE SCRIPTING LANGUAGE (CSS)-UNIT 2 (ARRAY) MRS. S.S. KADAM
--------------------------------------------------------------------------------------------------------------------------------------
Basic Array program [With Numeric values] :
<html>
<head>
<script language="javascript" type="text/javascript">
var arr1=[1,2,4,12,45,10];
document.write(arr1);
</script>
</head>
<body>
</body>
</html>
Output:
1,2,4,12,45,10
document.write(arr1);
</script>
</head>
<body>
</body>
</html>
Output:
1,”a”,4,”abcde”,45,10
2
CLIENT SIDE SCRIPTING LANGUAGE (CSS)-UNIT 2 (ARRAY) MRS. S.S. KADAM
--------------------------------------------------------------------------------------------------------------------------------------
2. By Creating instance of Array:
New keyword is used instance of array
Syntax:
Var array_name=new Array();
</body>
</html>
3
CLIENT SIDE SCRIPTING LANGUAGE (CSS)-UNIT 2 (ARRAY) MRS. S.S. KADAM
--------------------------------------------------------------------------------------------------------------------------------------
2.1.2 Initializing an Array:
Initialization is the process of assigning values to an array. While initializing an array,
all elements should be placed in parenthesis and separated by commas.
<body>
</body>
</html>
4
CLIENT SIDE SCRIPTING LANGUAGE (CSS)-UNIT 2 (ARRAY) MRS. S.S. KADAM
--------------------------------------------------------------------------------------------------------------------------------------
Program:Write a Javascript code to create and print an element in Array.
<html>
<head>
</script>
</head>
<body>
</body>
</html>
5
CLIENT SIDE SCRIPTING LANGUAGE (CSS)-UNIT 2 (ARRAY) MRS. S.S. KADAM
--------------------------------------------------------------------------------------------------------------------------------------
<html>
<head>
for(var a=0;a<arr1.length;a++)
{
document.write("<br>Element= "+arr1[a]);
}
</script>
</head>
<body>
</body>
</html>
6
CLIENT SIDE SCRIPTING LANGUAGE (CSS)-UNIT 2 (ARRAY) MRS. S.S. KADAM
--------------------------------------------------------------------------------------------------------------------------------------
Syntax:
Array_name.push(element1,element2,……,elementN);
<html>
<head>
arr1.push("Java");
arr1.push("VB","Python");
document.write("<br>Array length after adding new Element : "+arr1.length);
document.write("<br> Array after Adding element : "+arr1+"<br>");
</script>
</head>
<body>
</body>
</html>
Syntax:
Array_name.unshift(element1);
Array_name.unshift(element1,element2,……..,elementN);
7
CLIENT SIDE SCRIPTING LANGUAGE (CSS)-UNIT 2 (ARRAY) MRS. S.S. KADAM
--------------------------------------------------------------------------------------------------------------------------------------
<html>
<head>
<script language="javascript" type="text/javascript">
var arr1=new Array(2);
arr1[0]="C";
arr1[1]="C++";
document.write("Original Array Length :"+arr1.length);
document.write("<br>Origional Array Element : "+arr1+"<br>");
arr1.unshift("Java");
document.write("<br>Array length after adding new Element :
"+arr1.length);
document.write("<br> Array after Adding element : "+arr1+"<br>");
</script>
</head>
<body>
</body>
</html>
Syntax:
Array_name.sort();
8
CLIENT SIDE SCRIPTING LANGUAGE (CSS)-UNIT 2 (ARRAY) MRS. S.S. KADAM
--------------------------------------------------------------------------------------------------------------------------------------
Program:Write javascript code to print number array element by using sort
method
<html>
<head>
<script language="javascript" type="text/javascript">
var arr1=[8,6,9,4,2];
document.write("Array element before sorting.....");
document.write(arr1+"<br>");
arr1.sort();
document.write("Array element after sorting.....");
document.write(arr1);
</script>
</head>
<body>
</body>
</html>
9
CLIENT SIDE SCRIPTING LANGUAGE (CSS)-UNIT 2 (ARRAY) MRS. S.S. KADAM
--------------------------------------------------------------------------------------------------------------------------------------
Program:Write javascript code to print array element (string) by using sort
method
<html>
<head>
<script language="javascript" type="text/javascript">
var arr1=["xyz","pqr","abc"];
document.write("Array element before sorting.....");
document.write(arr1+"<br>");
arr1.sort();
document.write("Array element after sorting.....");
document.write(arr1);
</script>
</head>
<body>
</body>
</html>
10
CLIENT SIDE SCRIPTING LANGUAGE (CSS)-UNIT 2 (ARRAY) MRS. S.S. KADAM
--------------------------------------------------------------------------------------------------------------------------------------
Program: Write javascript code to print array element (all types) by using sort
method
<html>
<head>
</head>
<body>
</body>
</html>
11
CLIENT SIDE SCRIPTING LANGUAGE (CSS)-UNIT 2 (ARRAY) MRS. S.S. KADAM
--------------------------------------------------------------------------------------------------------------------------------------
The sort() function is used to sort the array in place in a given order
according to the compare() function. The only argument to this function
that is used to sort the elements according to different attributes and in
the different order.
Syntax:
Array_name.sort(CompareFunction);
</body>
</html>
Output:
12
CLIENT SIDE SCRIPTING LANGUAGE (CSS)-UNIT 2 (ARRAY) MRS. S.S. KADAM
--------------------------------------------------------------------------------------------------------------------------------------
Program: Write javascript code to sort array element in descending order.
<html>
<head>
<script language="javascript" type="text/javascript">
var arr1=new Array(9,23,34,8,10,2,45);
document.write("Array element before sorting:<br> "+arr1);
function compare(a,b)
{
return b-a;
}
arr1.sort(compare);
document.write("<br>Array element after sorting: <br>"+arr1);
</script>
</head>
<body>
</body>
</html>
Output:
13
CLIENT SIDE SCRIPTING LANGUAGE (CSS)-UNIT 2 (ARRAY) MRS. S.S. KADAM
--------------------------------------------------------------------------------------------------------------------------------------
Reverse() Method:
Reverse() method is used to display sorted list in reverse manner i.e.in
descending order. It will return the reversed single value of the array.
Syntax: array.reverse();
<html>
<head>
<script language="javascript" type="text/javascript">
var arr1=new Array(9,2,3,8,1,7,5);
document.write("Array element before sorting:<br> "+arr1);
var a=arr1.sort();
a.reverse();
document.write("<br>Array element after sorting: <br>"+a);
</script>
</head>
<body>
</body>
</html>
Output:
14
CLIENT SIDE SCRIPTING LANGUAGE (CSS)-UNIT 2 (ARRAY) MRS. S.S. KADAM
--------------------------------------------------------------------------------------------------------------------------------------
2.1.7 Combining an Array Elements into strings:
In javascript an array can be combined into string using following two
function:
1.Join() method
2.Concat() method
1.Join() method:
This function joins all elements of an array into a string
Syntax:
Array.join(seperator);
Here,separator is alike /,_,* etc to separate to each element of the array. If
seperatoris not mentioned,it will display list with comma as separator.This
fuction returns a string after joining all elements together.
<html>
<head>
<script language="javascript" type="text/javascript">
var arr1=new Array("C","C++","Java",);
document.write("Array element before sorting:<br> "+arr1);
var a=arr1.join();
</head>
<body>
</body>
</html>
15
CLIENT SIDE SCRIPTING LANGUAGE (CSS)-UNIT 2 (ARRAY) MRS. S.S. KADAM
--------------------------------------------------------------------------------------------------------------------------------------
2.Concat() method:
This function is used to join two or more array together in JavaScript.
This function returns a new string which is combination of different string
passed to it as arguments.
Syntax:
String.concat(string1,string2,string3,……);
<html>
<head>
<script language="javascript" type="text/javascript">
var arr1=new Array("C","C++","Java",);
document.write("Array element before concat method:<br> "+arr1);
var a=arr1.concat("VB");
document.write("<br>Array element after concat method: <br>"+a);
var a1=arr1.concat("PHP",".NET");
document.write("<br>Array element after concat method: <br>"+a1);
</script>
</head>
<body>
</body>
</html>
Output:
16
CLIENT SIDE SCRIPTING LANGUAGE (CSS)-UNIT 2 (ARRAY) MRS. S.S. KADAM
--------------------------------------------------------------------------------------------------------------------------------------
2.1.8: Changing Elements of an array:
1.Shift()
2.Pop()
3. Unshift()
4.Push()
1.Shift()
This method is used to remove first element from an array and returns the
removed single value of an array.
Syntax:
Array.shift();
<html>
<head>
<script language="javascript" type="text/javascript">
var arr1=new Array(1,2,3,4,5);
document.write("Array element before shift method: "+arr1);
var a=arr1.shift();
document.write("<br> Removed element after Shift method: "+a);
document.write("<br>Array Element after shift method: ",arr1);
</script>
</head>
<body>
</body>
</html>
Output:
17
CLIENT SIDE SCRIPTING LANGUAGE (CSS)-UNIT 2 (ARRAY) MRS. S.S. KADAM
--------------------------------------------------------------------------------------------------------------------------------------
2.Pop Method:
This method is used to remove last element and return the removed
element from an array.
Syntax:
Array.pop()
<html>
<head>
</head>
<body>
</body>
</html>
Output:
18
CLIENT SIDE SCRIPTING LANGUAGE (CSS)-UNIT 2 (ARRAY) MRS. S.S. KADAM
--------------------------------------------------------------------------------------------------------------------------------------
3.Splice method:
This method is used to add or remove the elements to or from the existing
array. It returns the removed elements from an array. This method also modifies
the original array.
Syntax:
Array.splice(start, delete, element1, element2, ….., elementn);
Start:It represents the index from where the method start to extract the
element.
Delete:It is optional.It represents the number of elements to be removed.
element1, element2, ….. , elementn : It is optional. It represent the elements to
be inserted.
Program: Write a javascript code to add element in array using splice method
<html>
<head>
</head>
<body>
</body>
</html>
Output:
19
CLIENT SIDE SCRIPTING LANGUAGE (CSS)-UNIT 2 (ARRAY) MRS. S.S. KADAM
--------------------------------------------------------------------------------------------------------------------------------------
<html>
<head>
</head>
<body>
</body>
</html>
Output:
20
CLIENT SIDE SCRIPTING LANGUAGE (CSS)-UNIT 2 (ARRAY) MRS. S.S. KADAM
--------------------------------------------------------------------------------------------------------------------------------------
In below example splice method start remove operation from 2 nd index position
till end.
<html>
<head>
</head>
<body>
</body>
</html>
Output:
21
CLIENT SIDE SCRIPTING LANGUAGE (CSS)-UNIT 2 (ARRAY) MRS. S.S. KADAM
--------------------------------------------------------------------------------------------------------------------------------------
Program: Write a javascript code to add as well as remove elements in array
using splice method.
<html>
<head>
</head>
<body>
</body>
</html>
Ouput:
22
CLIENT SIDE SCRIPTING LANGUAGE (CSS)-UNIT 2 (ARRAY) MRS. S.S. KADAM
--------------------------------------------------------------------------------------------------------------------------------------
2.1.9 Objects as Associative Array:
Associative array are dynamic objects that the user redefines as needed. When you assign
values to keys in a variable of type array, the array is transformed into an object, and it loses
the attributes and methods of array. We can create it by assigning a literal to a variable.
Syntax:
var arrayname={key1:’value1’,key2:’value2’};
Example:
var a={“name”:”sampada”,”rollno”:30};
Program1:
<html>
<body>
<script language="javascript" type="text/javascript">
var a={"name":"sampada","rollno":30,"class":"TYCM"};
document.write("<br>Name : "+a["name"]);
document.write("<br>Roll Number : "+a["rollno"]);
document.write("<br>Class : "+a["class"]);
</script>
</body>
</html>
Output:
23
CLIENT SIDE SCRIPTING LANGUAGE (CSS)-UNIT 2 (ARRAY) MRS. S.S. KADAM
--------------------------------------------------------------------------------------------------------------------------------------
We can create an associative array with the object reserved word, then assign key and values.
Program 2:
<html>
<body>
</body>
</html>
Output:
24