loading...
Cover image for All about javascript array methods..

All about javascript array methods..

dvlpr_roshan profile image Roshan kumar ・5 min read

Javascript all Array method

Array is very useful in almost all programming language, here we see javascript array method whom we used mostly so let's get started.

Array.join()

The Array.join() is used to converting whole array array to string & return a single string, if we use .join() method without any arguments then they separate element with ','

let arr = [1,2,3];          // Init an array
arr.join();                 // return => "1,2,3"
arr.join(" ");              // return => "1 2 3"
arr.join("");               // return => "123"

let arr1 = new Array(10);   // Init empty array with length 10
arr1.join('_');             // return => "__________"

Array.reverse()

The Array.reverse() method reverse the order of the elements of an array and return a reversed array.It's didn't take any arguments.

var a = [1,2,3];           // Init an array
a.reverse();               // return => [3,2,1]

Stings is also a part of an array.

Array.sort()

The Array.sort() method sort the element of an array. If we apply short method without any arguments, they sort ascending order. also this can take a function in form of arguments where we provide return value. If arguments is presents so they sort according to argument provided by user.

var a = [555,44,188,3];       // Init an array
a.sort()                      // now a is sorted in alphabetical order => [188,3,44,555]

// sort in descending order 
a.sort( (a,b) => a > b ? -1 : 1 );         
/* arguments in sort method is known as compareFunction 
there have firstEl & secondEl is parameters. this sort 
method sort like this [555,188,44,33] */

// sort in ascending order
a = ["ujjwal","devendar","roshan"];
a.sort( (a,b) => a > b ? -1 : 1);
/* sort => ["devendar","roshan","ujjwal"] */

Array.concat()

The Array.concat() method return combined array of original and passed by arguments. concat() method does not modify the original array.

let a = [1,2,3];              // Init an array
a.concat(4,6);                // return => [1,2,3,4,6]
a.concat([4,6]);              // return => [1,2,3,4,6]
a.concat([1,7],[7,2]);        // return => [1,2,3,1,7,7,2]
a.concat(4,[5,[7,2]]);        // return => [1,2,3,4,5,[7,2]]

Array.slice()

The Array.slice() method can take max two arguments and return a part of an array. It's didn't change original array value.If we don't provide any arguments then return exact same array.

let a = [1,2,3,4,5,6];             // Init an array
a.slice();                         // return same array => [1,2,3,4,5,6]
a.slice(0,4);                      // return => [1,2,3,4]
a.slice(4);                        // return => [5,6]
a.slice(1,-1);                     // return => [2,3,4,5]
a.slice(-4,-2);                    // return => [3,4]

Array.splice()

The Array..splice() method unlike slice(),concat can modify the original variable and return an array. If there have single argument they take as how many element you want to splice the array, & If there have two arguments so take as index value.

let a = [1,2,3,4,5,6,7,8,9,10];       // Init an array
a.splice(4);                          // return => [5,6,7,8,9,10] && a = [1,2,3,4]   
a.splice(1,2);                        // return => [2,3] && a = [1,4]  
a.splice(1,1);                        // return => [4] && a = [1] 

Array.push()

The Array.push() method is used to adding elements into array modify array and return length of array

let a = [1,2];           // Init an array
a.push(5,3);             // a is now => [1,2,5,3] && return => 4

Array.pop()

The Array.pop() method is used to remove last element of array and return last element

let a = [1,2,3,4,5];      // Init an array
a.pop()                   // a is now => [1,2,3,4] && return 5

Array.unshift()

The Array.unshift() is just like Array.push() method but this can insert elements from beginning, & like push() it's also return length of array

let a = [1];              // Init an array
a.unshift(14)             // a is now => [14,1] && return => 2
a.unshift([45,15],2)      // a is now => [[45,15],2,14,1] && return => 4

Array.shift()

The Array.shift() is just like Array.pop()</> method but delete element from beginning, & just like pop() it's also return deleted elements

let a = [1,2,3,4,5];        // Init an array
a.shift()                   // a is now => [2,3,4,5] && return 1

Array.toString()

The Array.toString() is used to convert array to string and separate element with ',' but not include '[' & ']' in string.

[1,2,3].toString()           // return => "1,2,3"
["a","b","c"].toString()     // return => "a,b,c"
[1,[2,'c']].toString()       // return => "1,2,c"

Array.toLocalString() is localized version of toString().

Array.forEach()

The Array.forEach() is used to invoking a function to specify for each element of an array which take a function with one arguments that is targeted element.

let arr = ["one","two",3];
arr.forEach(
     (elem)=>{
        console.log(elem);
     }
)

/* output of program is 
"one"
"two"
3
*/

Array.map()

The Array.map() is method of Array object with take a function, where have a arguments which target each elements same as .forEach() method & return a new modified array.

let a = [1,2,3];
b = a.map(
     function(x){
           return x+2  
      }
)
//now output b is:
//[3,4,5]

Array.filter()

The Array.filter() is to filtering data into array. the arguments of this function is same as .map() .forEach(), which take a function with boolean return. if function return true then elements are included in array.

let arr = [1,2,3,4,5,6,7,8,9,10];
let b = arr.filter(
    function(x){
           return x/2 == 0;        
       }
)
// now b is =>
//[2,4,6,8,10] 

Array.every()

The Array.every() is used to asking question about array is every elements is true when we apply given condition, & return boolean value (true,false)

let arr = [1,2,345,6,15,48];
let b = arr.every(
     function(x){
           return x < 50;
       }
)
// now b is => false

b = arr.every(
     function(x){
           return x < 500;
       }
)
// now b is => true

Array.some()

The Array.some() is used to asking question about array is any elements is true when we apply given condition, & return boolean value (true,false)

let arr = [8,2,345,6,15,48];
let b = arr.some(
     function(x){
           return x < 50;
       }
)
// now b is => true

b = arr.some(
     function(x){
           return x < 2;
       }
)
// now b is => true

Posted on by:

Discussion

markdown guide