Arrays in JavaScript

JavaScript array is used to store multiple value in a single variable. It stores a fixed-size sequential collection of elements of the same type.

Creating Array in JavaScript

There are 3 ways to create an array.

1- It can created by array literal
2- It can created by creating instance of Array directly (using new keyword)
3- It can created by using an Array constructor (using new keyword)


JavaScript array literal

var arrayname=[value1,value2.....valueN];
As you can see, values are contained inside [ ] and separated by , (comma).
Let's see the simple example of creating and using array in JavaScript.

<script type = "text/javascript">
var emp=["Imran","Amir","Irfan"];  
for (i=0;i<emp.length;i++){  
document.write(emp[i] + "
"); } </script>

Output of above code


JavaScript Array directly (new keyword)

The syntax of creating array directly is given below:
var arrayname=new Array();
Here, new keyword is used to create instance of array.
Let's see the example of creating array directly.

<script type = "text/javascript">
var i;  
var emp = new Array();  
emp[0] = "Imran";  
emp[1] = "Amir";  
emp[2] = "Irfan";  
  
for (i=0;i<emp.length;i++){  
document.write(emp[i] + "
"); } </script>

Output of above code


JavaScript array constructor (new keyword)

Here, you need to create instance of array by passing arguments in constructor so that we don't have to provide value explicitly.
The example of creating object by array constructor is given below.

<script type = "text/javascript">
var emp=new Array("Imran","Amir","Irfan");  
for (i=0;i<emp.length;i++){  
document.write(emp[i] + "
"); } </script>

Output of above code


JavaScript Array Methods

Let's see the list of JavaScript array methods with their description.

Methods Description
concat() This method returns a new array object that contains two or more merged arrays in JavaScript.
copywithin() This method copies the part of the given array with its own elements and returns the modified array in JavaScript.
entries() This method creates an iterator object and a loop that iterates over each key/value pair in JavaScript.
every() This method determines whether all the elements of an array are satisfying the provided function conditions in JavaScript.
flat() This method creates a new array carrying sub-array elements concatenated recursively till the specified depth in JavaScript.
flatMap() This method maps all array elements via mapping function, then flattens the result into a new array in JavaScript.
fill() This method fills elements into an array with static values in JavaScript.
from() This method creates a new array carrying the exact copy of another array element in JavaScript.
filter() This method returns the new array containing the elements that pass the provided function conditions in JavaScript.
find() This method returns the value of the first element in the given array that satisfies the specified condition in JavaScript.
findIndex() This method returns the index value of the first element in the given array that satisfies the specified condition in JavaScript.
forEach() This method invokes the provided function once for each element of an array in JavaScript.
includes() It checks whether the given array contains the specified element in JavaScript.
indexOf() This method searches the specified element in the given array and returns the index of the first match in JavaScript.
isArray() This method tests if the passed value ia an array in JavaScript.
join() This method joins the elements of an array as a string in JavaScript.
keys() This method searches the specified element in the given array and returns the index of the last match in JavaScript.
lastIndexOf() This method creates an iterator object that contains only the keys of the array, then loops through these keys in JavaScript.
map() This method calls the specified function for every array element and returns the new array in JavaScript.
of() This method creates a new array from a variable number of arguments, holding any type of argument in JavaScript.
pop() This method removes and returns the last element of an array in JavaScript.
push() This method adds one or more elements to the end of an array in JavaScript.
reverse() This method reverses the elements of given array in JavaScript.
reduce(function, initial) This method executes a provided function for each value from left to right and reduces the array to a single value in JavaScript.
reduceRight() This method executes a provided function for each value from right to left and reduces the array to a single value in JavaScript.
some() This method determines if any element of the array passes the test of the implemented function in JavaScript.
shift() This method removes and returns the first element of an array in JavaScript.
slice() This method returns a new array containing the copy of the part of the given array in JavaScript.
sort() This method returns the element of the given array in a sorted order in JavaScript.
splice() This method add/remove elements to/from the given array in JavaScript.
toLocaleString() This method returns a string containing all the elements of a specified array in JavaScript.
toString() This method converts the elements of a specified array into string form, without affecting the original array in JavaScript.
unshift() This method adds one or more elements in the beginning of the given array in JavaScript.
values() This method creates a new iterator object carrying values for each index in the array in JavaScript.







The Best

Comment here

If you have any query, if you want to know something about any of technical course related to computer science field, if you have any suggestion about relevant to uploaded content or if you anything wrong here (any mistake in content) than please contact us. Keep in mind, comment should be according to community guidelines.