JavaScript – Arrays


What is an array ?

Something which belongs to the same type is ordered in series is called as array.

What is an array in Javascript ?

An array in javascript is an object that represents a group of same-typed elements.

Why is array used in Javascript ?

An array is used to store a collection of elements of the same type.

If we want to iterate a series number of similar elements, which can be achieved by storing elements in array.

How to construct an array in Javascript ?

There are three ways to construct array, some of them are listed below,

1. Array literal

2. Creating instance of Array directly

3. Using an Array constructor

Javascript interview questions basic !!!

Click to Learn More about – Javascript online learning

1. Array Literal

Example :

<!DOCTYPE html>
<html>
	<head>
		<title>Our title</title>
		<script type="text/javascript">
			var a = ["Abdul","87","Male","Tamilnadu"];
			document.write("Name = "+a[0]+"<br/>");
			document.write("Age = "+a[1]+"<br/>");
			document.write("Sex = "+a[2]+"<br/>");
			document.write("Location = "+a[3]+"<br/>");
		
		</script>
	</head>
	<body></body>
</html>

Output :


Name = Abdul

Age = 87

Sex = Male

Location = Tamilnadu

2. Creating instance of Array directly

This can be done by using “new” keyword.

Example :

<!DOCTYPE html>
<html>
	<head>
		<title>Our title</title>
		<script type="text/javascript">
			var a = new Array();
			a[0] = "Abdul";
			a[1] = "Krishna";
			a[2] = "Gandhi";
			a[3] = "TamilNadu";
			a[4] = "Butterfly";
			a[5] = "Elephant";
			a[6] = "Flower";

			for(i=0;i<a.length;i++){
				document.write("Array value of a of " + i + " = "+ a[i] + "<br/>");
			}
		
		</script>
	</head>
	<body></body>
</html>

Output :

Array value of a of 0 = Abdul

Array value of a of 1 = Krishna

Array value of a of 2 = Gandhi

Array value of a of 3 = TamilNadu

Array value of a of 4 = Butterfly

Array value of a of 5 = Elephant

Array value of a of 6 = Flower

3. Using an Array constructor

This is done using “new” keyword.

Example :


<!DOCTYPE html>
<html>
	<head>
		<title>Our title</title>
		<script type="text/javascript">
			var a = new Array("Abdul","Krishna","Gandhi","TamilNadu","Butterfly","Elephant","Flower");
			for(i=0;i<a.length;i++){
				document.write("Array value of a of " + i + " = "+ a[i] + "<br/>");
			}
		
		</script>
	</head>
	<body></body>
</html>

Output :

Array value of a of 0 = Abdul

Array value of a of 1 = Krishna

Array value of a of 2 = Gandhi

Array value of a of 3 = TamilNadu

Array value of a of 4 = Butterfly

Array value of a of 5 = Elephant

Array value of a of 6 = Flower

Javascript has a lot of array methods, some of them are listed below.

SnoMethodDescription
1concat()This returns a new array object that contains two or more merged arrays.
2copywithin()This copies the part of the given array with its own elements and returns the modified array.
3every()This determines whether all the elements of an array are satisfying the provided function conditions.
4fill()This fills elements into an array with static values.
5filter()This returns the new array containing the elements that pass the provided function conditions.
6find()This returns the value of the first element in the given array that satisfies the specified condition.
7findIndex()This returns the index value of the first element in the given array that satisfies the specified condition.
8forEach()This invokes the provided function once for each element of an array.
9includes()This checks whether the given array contains the specified element.
10indexOf()This searches the specified element in the given array and returns the index of the first match.
11join()This joins the elements of an array as a string.
12lastIndexOf()This searches the specified element in the given array and returns the index of the last match.
13map()This calls the specified function for every array element and returns the new array
14pop()This removes and returns the last element of an array.
15push()This adds one or more elements to the end of an array.
16reverse()This reverses the elements of given array.
17shift()This removes and returns the first element of an array.
18slice()This returns a new array containing the copy of the part of the given array.
19sort()This returns the elements of the given array in a sorted order.
20splice()This add/remove elements to/from the given array.
21unshift()This adds one or more elements in the beginning of the given array.