JavaScript Array and Array Methods [cheatsheet]

JavaScript Array methods

JavaScript is a popular client side programming language.

Just like any other language, JS also contains Arrays and a wide range of inbuilt methods to make the developers work easy. In this short tutorial, we’ll understand what is an array and see some commonly used JavaScript Array methods.

Let’s begin.

What is an Array?

JavaScript Array or an Array is a linear data structure of homogenous elements stored in an indexed format.

Simply put, an array contains a collection of values in a sequential format where the index starts from 0.

How to create an Array in JavaScript:

const students = ["John", "Mike", "Alisha", "Taylor"];

How to access elements of an Array:

const element = array[index];

Most commonly used Array methods in JavaScript:

Array methods are inbuilt methods(functions) that helps in performing actions on an Array.

  • push() – Used to insert an element at the end of the array.
  • unshift() – Used to insert an element at the beginning of the array.
  • pop() – Remove an element from the end of the array.
  • shift() – Remove an element from the beginning of the array.
  • slice() – Create a shallow copy of an array.
  • Array.isArray() – Determine if a value is an array.
  • length – Determine the size of an array.
  • concat() – This method merges one or more arrays and returns a merged array.
  • join() – Join method joins all the elements of the array using a separator and returns a string. The default separator is a comma. We can pass our own separator as well. Example: join(‘<‘)
  • fill() – fills an array with a static value
  • includes() – Used to check the presence of an element in an array.
  • indexOf() – Used to know the index position of an element in an array.
  • lastIndexOf() – This lets you find the index of the last occurrence of an element in the array.
  • reverse() – this method reverses the elements’ positions in the array. The first element becomes last and the last element becomes first.
  • sort() – The sort method converts the element types into strings and then sorts them in ascending order.

JavaScript Array static methods:

  • Array.isArray() – used to check if the element is an Array element.
  • Array.from() – used to convert a collection(object-like entity) into an array, so we can perform Array methods on the collection.
  • Array.of() – used to create a new array using any number of elements of any type.

JavaScript Array Iterator methods:

  • filter() – creates and returns a new array that satisfies the condition in the function passed.
  • map() – creates a new array by iterating the array elements and applying logic passed in as function argument.
  • some() – returns a boolean value if an element passes a condition passed in the function.
  • find() – returns the first matched element that matches the condition passed in the function.
  • every() – used to detect if every element of the array satisfies the condition passed. Returns a boolean value.

Towards the end:

In this short tutorial, we have seen what an array is and how we can use the listed methods to perform simple actions on our array.

If you like this tutorial, please share the article that might help others as well.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top