DEV Community

Kavin Loyola S
Kavin Loyola S

Posted on

Array Methods in JS

Array Methods

JavaScript provides a variety of built-in methods to manipulate and traverse arrays efficiently. These methods enhance code readability and productivity by offering functionalities like adding, removing, and transforming elements, as well as searching, sorting, and iterating through array elements.

  • length

The length property returns the length (size) of an array. and it can also be used to set the length of an array.


const vijay = ['Jananayagan','Goat','Leo','Beast'];
let size = vijay.length;
console.log(size);

o/p - 4

Enter fullscreen mode Exit fullscreen mode
  • toString()

this method returns the elements of an array as a comma separated string,
Every JavaScript object has a toString() method.


const vijay = ['Jananayagan','Goat','Leo','Beast'];
let movies = vijay.toString();
console.log(movies);

o/p - Jananayagan,Goat,Leo,Beast

Enter fullscreen mode Exit fullscreen mode
  • at()

this method returns an indexed element from an array, and it returns the same as [].it allows negative bracket indexing like [-1] to access elements from the end of an object / array / string.


const vijay = ['Jananayagan','Goat','Leo','Beast'];
let movies = vijay.at(-2);
console.log(movies);

o/p - Leo

Enter fullscreen mode Exit fullscreen mode
  • join()

The join() method also joins all array elements into a string, and It behaves just like toString(), but in addition you can specify the separator:

const vijay = ['Jananayagan','Goat','Leo','Beast'];
let movies = vijay.join('-');
console.log(movies);

o/p - Jananayagan-Goat-Leo-Beast

Enter fullscreen mode Exit fullscreen mode
  • pop()

The pop() method removes the last element from an array, and it returns the value that was "popped out".


const vijay = ['Jananayagan','Goat','Leo','Beast'];
let movies = vijay.pop();
console.log(vijay);
console.log(movies);

o/p - (3) ['Jananayagan', 'Goat', 'Leo'] 
      Beast

Enter fullscreen mode Exit fullscreen mode
  • push()

The push() method adds a new element to an array (at the end).


const vijay = ['Jananayagan','Goat','Leo','Beast'];
let movies = vijay.push('leo 2');
console.log(vijay);

o/p - (5) ['Jananayagan', 'Goat', 'Leo', 'Beast', 'leo 2']

Enter fullscreen mode Exit fullscreen mode
  • shift()

the shift() method removes the first array element and "shifts" all other elements to a lower index, and it returns the value that was "shifted out".


const vijay = ['Jananayagan','Goat','Leo','Beast'];
let movies = vijay.shift();
console.log(vijay);
console.log(movies);

o/p - (3) ['Goat', 'Leo', 'Beast']
      Jananayagan

Enter fullscreen mode Exit fullscreen mode
  • unshift()

The unshift() method adds a new element to an array (at the beginning), and "unshifts" older elements, and it returns the new array length.


const vijay = ['Jananayagan','Goat','Leo','Beast'];
let movies = vijay.unshift('Leo 2');
console.log(vijay);

o/p - (5) ['Leo 2', 'Jananayagan', 'Goat', 'Leo', 'Beast']

Enter fullscreen mode Exit fullscreen mode
  • Array.isArray()

The Array.isArray() method in JavaScript is used to check whether a given value is an array. It returns a boolean (true or false).

const vijay = ['Jananayagan','Goat','Leo','Beast'];
console.log(Array.isArray(vijay));

o/p - true

Enter fullscreen mode Exit fullscreen mode
  • delete()

Using delete() leaves undefined holes in the array, instead Use pop() or shift() methods.


const vijay = ['Jananayagan','Goat','Leo','Beast'];
delete vijay[0];
console.log(vijay);

o/p - (4) [empty, 'Goat', 'Leo', 'Beast']

Enter fullscreen mode Exit fullscreen mode
  • concat()

The concat() method creates a new array by merging (concatenating) existing arrays, and it does not change the existing arrays. It always returns a new array, and it can take any number of array arguments.


const vijay = ['Jananayagan','Goat','Leo','Beast'];
const year = [2026,2024,2023,2022];
const movies=vijay.concat(year);
console.log(movies);

o/p - (8) ['Jananayagan', 'Goat', 'Leo', 'Beast', 2026, 2024, 2023, 2022]

Enter fullscreen mode Exit fullscreen mode
  • copyWithin()

The copyWithin() method in JavaScript is used to copy a part of an array to another position within the same array, without changing its length. It overwrites existing elements rather than adding new ones.


const vijay = ['Jananayagan','Goat','Leo','Beast'];
let movies=vijay.copyWithin(2,0);
console.log(vijay);

o/p - (4) ['Jananayagan', 'Goat', 'Jananayagan', 'Goat']

Enter fullscreen mode Exit fullscreen mode

here in the above one (2,0) 2 is target - where the copied elements will be placed, 0 is start - the index where copying begins.

  • splice()

The splice() method can be used to add new items to an array.

const vijay = ['Jananayagan','Goat','Leo','Beast'];
vijay.splice(2,0,'Theri','Master');
console.log(vijay);

o/p - (6) ['Jananayagan', 'Goat', 'Theri', 'Master', 'Leo', 'Beast']

Enter fullscreen mode Exit fullscreen mode

The first parameter (2) defines the position where new elements should be added (spliced in).The second parameter (0) defines how many elements should be removed.

The splice() method returns an array with the deleted items.

const vijay = ['Jananayagan','Goat','Leo','Beast'];
vijay.splice(2,2,'Theri','Master');
console.log(vijay);

o/p - (4) ['Jananayagan', 'Goat', 'Theri', 'Master']

Enter fullscreen mode Exit fullscreen mode

Top comments (0)