9 JavaScript Array Methods You Should Know

ยท

2 min read

9  JavaScript Array Methods You Should Know

Let's understand one by one

  • push() method

push() method adds the new element to the end of an existing array.

let arr = [ "Sumit",  "Aditiya", "Shubham"];

arr.push("Ayushi");
console.log(arr);     // Output:  ["Sumit", "Aditiya", "Shubham", "Ayushi"]
  • pop() method

pop() method removes the last element of an existing array.

let arr = [ "Sumit",  "Aditiya", "Shubham"];

arr.pop();

console.log(arr); // Output: ["Sumit", "Aditiya"]
  • include() method

include() method inspects whether the given array holds a specified element or not

let arr = [ "Sumit",  "Aditiya", "Shubham"];

console.log(arr.includes("Sumit"));  // Output: true
  • reverse() method

It reverses the order of an array.

let arr = [ "Sumit",  "Aditiya", "Shubham"];

console.log(arr.reverse()); // Output: ["Shubham", "Aditiya", "Sumit"]
  • join() method

It unifies all items of an array into a string

let arr = [ "Sumit",  "Aditiya", "Shubham"];

console.log(arr.join());    // Output: Sumit,Aditiya,Shubham
  • shift() method

It takes off the first element of an array

let arr = [ "Sumit",  "Aditiya", "Shubham"];

console.log(arr.shift());  // Output: Sumit
  • unshift() method

It adds a new element to the starting of an array

let arr = [ "Sumit",  "Aditiya", "Shubham"];
arr.unshift("Radha");
console.log(arr);  // Output:  ["Radha", "Sumit", "Aditiya", "Shubham"]
  • slice() method

slice() method cut out selected items of an array. It takes two parameters: starting and ending position

let arr = [ "Sumit",  "Aditiya", "Shubham"];

arr.slice(1,2);  // Output: ["Aditiya"]
  • toString() method

It changes an array to a string

let arr = [1, 2, 3, 4, 5];
arr.toString(); // Output

Thanks for reading ๐Ÿ˜‡