ARRAYS IN JAVASCRIPT


Arrays are single variables in which many datas of single type are inserted.

Creating an array in javascript

To create array we first want to declare a variable then we need to enter desired data in it.In the example given below declaration as well as data is filled simultaneously.
<script>
var objects=["book","copy","pen","pencil"];
</script>

Accessing an array in javascript

To access an array element in javascript one needs to mention position of certain element in given array.
Javastring starts counting from 0 and hence first element of every array is consider to be at 0th position in javascript.
<!DOCTYPE html>
<html>
<head>
<title>
JAVASCRIPT
</title>
</head>
<body>
<p id="out">
</p>
<script>
var objects=["book","copy","pen","pencil"];
document.getElementById
("out").innerHTML=objects[2];
</script>
</body>
</html>

Output:

Finding length of array in javascript

To find length of array we use objects.length.This counts elements of array from left to right.
<!DOCTYPE html>
<html>
<head>
<title>
JAVASCRIPT
</title>
</head>
<body>
<p id="out">
</p>
<script>
var objects=["book","copy","pen","pencil"];
o=objects.length;
document.getElementById
("out").innerHTML=o;
</script>
</body>
</html>

Output:

Sorting an array in alphabetical order in javascript

To sort an array we use objects.sort() method.This method sorts the array according to alphabetical prefference.
<!DOCTYPE html>
<html>
<head>
<title>
JAVASCRIPT
</title>
</head>
<body>
<p id="out">
</p>
<script>
var objects=["book","copy","pen","pencil"];
o=objects.sort();
document.getElementById
("out").innerHTML=o;
</script>
</body>
</html>

Output:

Removing/DELETING or popping an element from an array in javascript

To remove or delete an element from array in javascript we use objects.pop() functions.This function deletes one element from extreme right hand side.
<!DOCTYPE html>
<html>
<head>
<title>
JAVASCRIPT
</title>
</head>
<body>
<p id="put">
</p>
<script>
var objects=["book","copy","pen","pencil"];
objects.pop();
document.getElementById
("put").innerHTML=objects;
</script>
</body>
</html>

Output:

Inserting or pushing an element in an array using javascript

To insert an element in array we use objects.push() function.This function inserts new element in an array on the extreme right hand side.
<!DOCTYPE html>
<html>
<head>
<title>
JAVASCRIPT
</title>
</head>
<body>
<p id="put">
</p>
<script>
var objects=["book","copy","pencil"];
objects.push(eraser);
document.getElementById
("put").innerHTML=objects;
</script>
</body>
</html>

Output:

Shifting the entire array to the left side.

shift() method in javascript.
This method never shows the extreme left positioned data in output since it moves to position beyond zero.
<!DOCTYPE html>
<html>
<head>
<title>
JAVASCRIPT
</title>
</head>
<body>
<p id="put">
</p>
<script>
var objects=["book","copy","pencil"];
objects.shift();
document.getElementById
("put").innerHTML=objects;
</script>
</body>
</html>

Output:

Unshifting the entire array.

objects.unshift() method is used to enter a new data on extreme left handside of the array.This method moves the entire array to the right handside and makes an empty space on right handside.
unshift() method in javascript.
<!DOCTYPE html>
<html>
<head>
<title>
JAVASCRIPT
</title>
</head>
<body>
<p id="put">
</p>
<script>
var objects=["book","pencil"];
objects.unshift("eraser");
document.getElementById
("put").innerHTML=objects;
</script>
</body>
</html>

Output:

Reversing an array in javascript.

To reverse an array we use objects.reverse() function.This function reverses the array.
<!DOCTYPE html>
<html>
<head>
<title>
JAVASCRIPT
</title>
</head>
<body>
<p id="put">
</p>
<script>
var objects=["book","pencil","eraser"];
objects.reverse();
document.getElementById
("put").innerHTML=objects;
</script>
</body>
</html>

Output:

Joining two arrays using javascript.

Undergiven example shows how to combine two arrays together.we use to concat these two arrays together.
<!DOCTYPE html>
<html>
<head>
<title>
JAVASCRIPT
</title>
</head>
<body>
<p id="put">
</p>
<script>
var addarray=["book","eraser"];
var newarray=["pen","pin"];
var x=addarray.concat(newarray);
document.getElementById
("putipets").innerHTML=x;
</script>
</body>
</html>

Output: