JavaScript and ES6 Array Available Methods.

Array Methods
pop() Remove the last element from an array
push()

Add a new element to the end from an array.

toString() Convert array to string.
join() Join the elements of an array into a string.
splice() Add and remove items from an array.
sort() Sort array alphabetically or numerically.
shift() remove the first element from an array.
unshift() Adds a new element to the beginning of an array.
reverse() Reverse the order of the elements in an array.
concat() Joins two or more arrays and makes a new one.
slice() Select elements in an array and make a new one. It can take one or two arguments.
filter() Filter the array, according our conditions.
find() Creates a new array that satisfy a condition
forEach() Permits the call of a function on each element of an array in an easy way.
map() Creates an array by calling a specific function on each element in the original array.
reduce() Reduces the array to a single value.
some() Checks whether at least one element of the array matches the given predicate. if none of the array elements
every() Checks whether all elements of the array match the predicate:
FindIndex() Array Find first occurrence position from an array.
Include() Returns true if an contains a given element; Otherwise, it returns false.
IndexOf() Array Find the first occurrence position of an array And returns the index of the first occurrence of the element. If the element is not found, -1 return.
lastIndexOf() Smilar to the indexOf() , but this method is used to find the last occurrence position of an array. It returns -1 if it cannot find the element.
isArray() Check whether an object is an array. This function returns true if the object is an array, otherwise return false.

1: pop()

pop() method is used to remove the last element from an array.

The method returns the value of the removed item.

Example-

 
var lang = ["PHP", "Python", "Java", "C"];
lang.pop();    

Results:

Output
// New array: PHP, Python, Java

2: push()

push() method is used to add a new element to the end from an array.

Example-

 
var lang = ["PHP", "Python", "Java", "C"];
lang.push("JavaScript");        

Results:

Output
// New array: PHP, Python, Java, C, JavaScript

3: toString()

toString() method is used to convert array to string.

Example-

 
var lang = ["PHP", "Python", "Java", "C"];
lang.toString();

Results:

// output
PHP, Python, Java, C

4: join()

join() method is used to join the elements of an array into a string.

In otherwords, The "join()" method puts all the elements of the array into a string list. This method difference from "toString()" method.

Example-

 
var lang = ["PHP", "Python", "Java", "C"];
lang.join(" - ");

Results:

// output
PHP - Python - Java - C

5: splice()

splice() method is used to add and remove items from an array.

syntax
================
array.splice(index, howMany, [element1][, …, elementN]);
  • index − Index param specifies where a new item should be inserted.
  • howMany − An integer indicating the number of old array elements to remove.
  • If howMany set to 0, no items will be removed in array list.

Example-

 
var lang = ["PHP", "Python", "Java", "C"];
lang.splice(2, 0, "Javascript", "Rust");

Results:

// Output
new array: PHP, Python, Javascript, Rust, Java, C

6: sort()

sort() method either alphabetic or numeric sorts an array.

Example-

 
var lang = ["PHP", "Python", "Java", "C"];
lang.sort();

Results:

// Outuput
array: C, Java, PHP, Python

7: shift()

shift() method is used to remove the first element from an array.

Example-

 
var lang = ["PHP", "Python", "Java", "C"];
lang.shift();

Results:

// Output
new array: Python, Java, C

8: unshift()

unshift() method adds a new element to the beginning of an array.

Example-

 
var lang = ["PHP", "Python", "Java", "C"];
cars.unshift("JavaScript");    

Results:

//Output 
New array: JavaScript, PHP, Python, Java, C

9: reverse()

The method is used for reverses the order of the elements in an array.

Example-

 
var lang = ["PHP", "Python", "Java", "C"];            
lang.reverse();  

Results:

// Outuput
array: C, Java, PHP, Python

10: concat()

concat() method joins two or more arrays and makes a new one.

Example-

 
var lang = ["PHP", "Python", "Java", "C"]; 
var newlang = ["JavaScript", "Rust"]; 
var join = lang.concat(newlang);   

Results:

// Output
New array: PHP, Python, Java, C, JavaScript, Rust

11: slice()

Results:slice() array() method is used to selected elements in an array and make a new one. It can take one or two arguments.

Example-

 
var lang = ["PHP", "Python", "Java", "C", "JavaScript"];
var lang = cars.slice(1, 4); 

Results:

//Output 
New Array : Python, JavaScript

12: filter()

filter() method is used to filter the array, according our conditions.

Example-

const lessThanTen = value => value < 10;
let filter = [105164712].filter(lessThanTen);
console.log('filter='filter);

 

Results:

Output
//new array: 5,4,7

In this example the function filter() creates a new array. Those elements that satisfy the condition checked by isCheck() function.

13: find()

find() method is used find the first element of an array.

Example-

 
function isCheck(value) {
  return value >= 10;
}
 
var find= [10, 5, 16, 4, 7, 12].find(isCheck);

Results:

Output
// 12

14: forEach()

forEach method permits the call of a function on each element of an array in an easy way.

Example-

 
var num = [18, 12, 10, 15];
num.forEach(function(item) {
   document.writeln(item);
});

Results:

Output
// return

18 12 10 15

15: map()

map() method, creates an array by calling a specific function on each element in the original array.

Example-

 
var numbers = [4, 9, 16, 25];
var x = numbers.map(Math.sqrt);
console.log(x);

Results: /p>

 Output
// return   
2,3,4,5 

16: reduce()

reduce() method reduces the array to a single value.

Example-

 
var numArr = [
        {  name: 'a', num: 50},
        {  name: 'b', num: 50},
        {  name: 'c', num: 75},
        {  name: 'd', num: 35},
        {  name: 'e', num: 25 },
        {  name: 'f', num: 40 },
    ];
 
    var sum = numArr.reduce(function (total, currentValue) {
        return total + currentValue.num;
    }, 0);
 
    console.log( "javascript- Sum of the array value is :- " + sum );

Results:

Output
// return   
275

17: some()

some() method checks whether at least one element of the array matches the given predicate. if none of the array elements match the predicate, then it will return false

Example-

 
var nums = [3, 18, 19, 20, 25];
 
function checkNumber(num) {
  return num >= 25;
}
 
console.log(nums.some(checkNumber));

Results:

Output
// return   
True

18: every()

every() method checks whether all elements of the array match the predicate:

Example-

 
var nums = [3, 18, 19, 20, 25];
 
function checkNumber(num) {
  return num >= 3;
}
 
console.log(nums.every(checkNumber));

Results:

 // return   
true

19: findIndex()

findIndex() method is used to find first occurrence position from an array.

 
let numbers = [1, 2, 3, 4, 5];
  
let res = numbers.findIndex(e => e % 2 == 1);
 
console.log(res); //

Results:

 // return   
0

20: include()

includes() method returns true if an contains a given element; Otherwise, it returns false.

 
[1,2,3].includes(2); // true
[1,2,3].includes(4); // false
[1,2,3].includes(1,1); // false

Results:

 // return   
true
false
false

21: indexOf()

indexOf() method is used to find the first occurrence position of an array And returns the index of the first occurrence of the element. If the element is not found, -1 return.

 
var arr = [10, 20, 30, 40, 50, 70, 10, 50];
console.log(arr.indexOf(10)); // 0
console.log(arr.indexOf(50)); // 4
console.log(arr.indexOf(70)); // 5

Results:

 // return   
0
4
5

22: lastIndexOf()

This is similar to the indexOf() method, but one difference in these. array lastIndexOf() method is used to find the last occurrence position of an array. It returns -1 if it cannot find the element.

 
var arr = [10, 20, 30, 40, 50, 70, 10, 40];
console.log(arr.lastIndexOf(10)); // 6
console.log(arr.lastIndexOf(40)); // 7

Results:

 // return   
6
7

23: isArray()

isArray() method is used to check whether an object is an array. This function returns true if the object is an array, otherwise return false.

 
var arr = [5, 3, 10, 1, 6, 12]
 
console.log("Retrun value = " + Array.isArray(arr));