DEV Community

Cover image for BASIC ARRAY METHODS
Vinoth Kumar
Vinoth Kumar

Posted on

BASIC ARRAY METHODS

BASIC ARRAY METHODS IN JS

To perform common tasks efficiently, JavaScript provides a wide variety of array methods. These methods allow you to add, remove, find, and transform array elements with ease.

METHODS IN ARRAY:

  • Array length
  • Array toString()
  • Array at()
  • Array join()
  • Array pop()
  • Array push()
  • Array shift()
  • Array unshift()

Array length: An array returns the number of elements in the array. It automatically updates as elements are added or removed.

EXAMPLE:

const fruits = ["Banana", "Orange", "Apple", "Mango"];
let size = fruits.length;
console.log(size);
Enter fullscreen mode Exit fullscreen mode

Array toString(): This method returns the elements of an array as a comma separated string.

EXAMPLE:

const fruits = ["Banana", "Orange", "Apple", "Mango"];
let myList = fruits.toString();
console.log(myList);
Enter fullscreen mode Exit fullscreen mode

Array at():

  • This at() method returns an indexed element from an array.
  • This at() method returns the same as [].

EXAMPLE:

const fruits = ["Banana", "Orange", "Apple", "Mango"];
let fruit = fruits.at(2);
console.log(sruit);
Enter fullscreen mode Exit fullscreen mode

Array join(): It creates and returns a new string by concatenating all elements of an array. It uses a specified separator between each element in the resulting string.

EXAMPLE:

let a = ["HTML", "CSS", "JS", "React"];
console.log(a.join('|'));
Enter fullscreen mode Exit fullscreen mode

Array pop(): It is used to remove elements from the end of an array.

EXAMPLE:

let a = [20, 30, 40, 50];
a.pop();
console.log(a);
Enter fullscreen mode Exit fullscreen mode

Array push(): It is used to add an element at the end of an Array. As arrays in JavaScript are mutable objects, we can easily add or remove elements from the Array.

EXAMPLE:

let a = [10, 20, 30, 40, 50];
a.push(60);
a.push(70, 80, 90);
console.log(a);
Enter fullscreen mode Exit fullscreen mode

Array shift(): It is used to remove elements from the beginning of an array.

EXAMPLE:

let a = [20, 30, 40, 50];
a.shift();
console.log(a);
Enter fullscreen mode Exit fullscreen mode

Array unshift(): is used to add elements to the front of an Array.

EXAMPLE:

let a = [20, 30, 40];
a.unshift(10, 20);
console.log(a);
Enter fullscreen mode Exit fullscreen mode

Top comments (0)