Home >>Javascript Tutorial >Javascript Array Methods

Javascript Array Methods

Javascript Array Methods

The JavaScript array methods and various properties that help the developers to handle arrays with efficiency and easiness. One can get the value of a property and methods by specifying arrayname.property and the output of a method by specifying arrayname.method().

There are various uses of the array methods from joining two arrays to creating an array from the object. They are basically used to store multiple values in a single variable.

There are various methods of arrays in JavaScript and they are known to perform various operations in the JavaScript language.

Array Methods in JavaScript

Here is the list of Array methods in JavaScript along with their description and Examples

Methods Description
concat() This method returns a new array object that contains two or more merged arrays.
copywithin() This method copies the part of the provided array with its own elements and returns the modified array.
every() This method determines whether all the elements of an array are satisfying the given function conditions.
fill() This method fills elements into an array with static values.
filter() This method returns the new array containing the elements that pass the given function conditions.
find() This method returns the value of the first element in the provided array that satisfies the specified condition.
findIndex() This method returns the index value of the first element in the provided array that satisfies the specified condition.
forEach() This method invokes the provided function once for each element of an array.
includes() This method checks whether the given array contains the specified element.
indexOf() This method searches the specified element in the provided array and returns the index of the first match.
join() This method joins the elements of an array as a string.
lastIndexOf() This method searches the specified element in the provided array and returns the index of the last match.
map() This method calls the specified function for every array element and returns the new array
pop() This method removes and returns the last element of an array.
push() This method adds one or more elements to the end of an array.
reverse() This method reverses the elements of provided array.
shift() This method removes and returns the first element of an array.
slice() This method returns a new array containing the copy of the part of the provided array.
sort() This method returns the element of the provided array in a sorted order.
splice() This method add/remove elements to/from the provided array.
unshift() This method adds one or more elements in the beginning of the provided array.

Here are the syntax and examples of the methods mentioned in the above table:

1. Array Concat() method in JavaScript

Syntax:

array.concat(arr1,arr2,....,arrn)  

Here is an example to easily understand the Concat() method:

<script>
var arr1=["hey","How","are"];
var arr2=["You","1","2"];
var result=arr1.concat(arr2);
document.writeln(result);
</script>
Output :
hey,How,are,You,1,2

2. Array CopyWithin() method in JavaScript

Syntax :

 array.copyWithin(target, start, end) 

Here is an example to easily understand the CopyWithin() method:

<script>
var arr=["PHPTPOINT","BEST","PHP TRAINING","INSTITUTE"]
// place from 0th position, the elements between 1st and 3rd position.
var result=arr.copyWithin(0,1,3);
document.writeln(result);
</script>
Output :
BEST,PHP TRAINING,PHP TRAINING,INSTITUTE

3. Array Every() method in JavaScript

Syntax :

array.every(callback(currentvalue,index,arr),thisArg)

Here is an example to easily understand the Every() method:

<script>
var marks=[10,50,25,97,60];
function check(value)
{
  return value>50;  //return false, as marks[4]=20
}
document.writeln(marks.every(check));
</script>
Output :
false

4. Array fill() method in JavaScript

Syntax :

arr.fill(value[, start[, end]])  

Here is an example to understand the fill() method:

<script>
var arr=["Java","PHP","Python"];
var result=arr.fill("PHPTPOINT");
document.writeln(arr);
</script>
Output :
PHPTPOINT,PHPTPOINT,PHPTPOINT

5. Array Filter() method in JavaScript

Syntax :

array.filter(callback(currentvalue,index,arr),thisArg 

Here is an example to easily understand the filter() method:

<script>
var marks=[56,58,95,17,30];
function check(value)
{
  return value>50;  
}
document.writeln(marks.filter(check));
</script>
Output :
56,58,95

6. Array Find() method in JavaScript

Syntax :

 array.find(callback(value,index,arr),thisArg) 

Here is an example to easily understand the find() method:

<script>
var arr=[7,20,15,30,95];
var result=arr.find(x=>x>50);
document.writeln(result)
</script>
Output :
95

7. Array Find Index() Method in JavaScript

Syntax :

 array.findIndex(callback(value,index,arr),thisArg) 

Here is an example of find index() method:

<script>
var arr=[50,62,99,18,13];
var result=arr.findIndex(x=>x>50);
document.writeln(result)
</script>
Output :
1

8. Array forEach() method in JavaScript

Syntax :

array.forEach(callback(currentvalue,index,arr),thisArg)   

Here is an example for understanding the forEach() method:

<script>
var arr = ['PHPTPOINT', 'is', 'the best'];
arr.forEach(function(fetch) {
  document.writeln(fetch);
});
</script>
Output :
PHPTPOINT is the best

9. Array includes() method in JavaScript

Syntax :

array.includes(element,start)  

Here is an example for understanding the includes() method:

<script>
var arr=["20","90","100"]
var result=arr.includes("10");
document.writeln(result);
</script>
Output :
false

10. Array indexOf() method in JavaScript

Syntax :

array.indexOf(element,index)  

Here is an example to understand the indexOf() method:

<script>
var arr=["20","21","23","95","68"];
var result= arr.indexOf("21");
document.writeln(result);
</script>
Output :
1

11. Array join() method in JavaScript

Syntax :

 array.join(separator) 

Here is an example to understand the join() method:

<script>
var arr=["Ferrari","possess","6000CC"]
var result=arr.join()
document.write(result);
</script>
Output :
Ferrari,possess,6000CC

12. Array lastIndexOf() method in JavaScript

Syntax :

array.lastIndexOf(element,index)  

Here is an example to understand the lastIndexOf() method:

<script>
var arr=["20","25","25","95","80"];
var result= arr.lastIndexOf("25");
document.writeln(result);
</script>
Output :
2

13. Array map() method in JavaScript

Syntax :

array.map(callback(currentvalue,index,arr),thisArg)

Here is an example to understand the map() method:

<script>
var arr=[5.1,9.9,7.7];
var result=arr.map(Math.round);
document.writeln(result);
</script>
Output :
5,10,8

14. Array pop() method in JavaScript

Syntax :

 array.pop()  

Here is an example to understand the pop() method:

<script>
var arr=["BEST","IS THE","PHPTPOINT"];
var len=arr.length;
for(var x=1;x<=len;x++)
  {
document.writeln("Extracted element: "+arr.pop()+"<br>");
  }
</script>
Output :
Extracted element: PHPTPOINT
Extracted element: IS THE
Extracted element: BEST

15. Array push() method in JavaScript

Syntax :

 array.push(element1,element2....elementn)  

Here is an example to understand the push() method:

<script>
var arr=["Hey","There"];
arr.push("How's you?");
document.writeln(arr);
</script>
Output :
Hey,There,How's you?

16. Array reverse() method in JavaScript

Syntax :

 array.reverse() 

Here is an example to understand the reverse() method:

<script>
var arr=["This","is","in reverse"];
var rev=arr.reverse();
document.writeln(rev);
</script>
Output :
in reverse,is,This

17. Array shift() method in JavaScript

Syntax :

 array. shift()  

Here is an example to understand the shift() method:

<script>
var arr=["Hello","THIS IS","SHIFT"];
var result=arr.shift();
document.writeln(result);
</script>
Output :
Hello

18. Array Slice() method in JavaScript

Syntax :

 array.slice(start,end)  

Here is an example to understand the slice() method:

<script>
var arr=["Hello","This","Is","Slice"]
var result=arr.slice(2,3);
document.writeln(result);
</script>
Output :
Is

19. Array sort() method in JavaScript

Syntax :

array.sort(compareFunction)

Here is an example to understand the sort() method:

<script>
var arr=["Apple","Nokia","Boolean","Lava"]
var result=arr.sort();
document.writeln(result);
</script>
Output :
Apple,Boolean,Lava,Nokia

20. Array splice() method in JavaScript

Syntax :

array.splice(start,delete,element1,element2,?,elementn) 

Here is an example to understand the splice() method:

<script>
var arr=["Here","is","the"];
var result=arr.splice(3,1,"placement")
document.writeln(arr);
</script>
Output :
Here,is,the,placement

21. Array Unshift() method in JavaScript

Syntax :

array. unshift(element1,element2,....,elementn)  

Here is an example to understand the unshift() method:

<script>
var arr=["Is","Here"];
var result=arr.unshift("Shift");
document.writeln(arr); 
</script>
Output :
Shift,Is,Here

No Sidebar ads