Home Videos Exercises MCQ Q&A Quiz E-Store Services Blog Sign in Appointment Payment

JavaScript Array Methods

Adds one or more elements to the front of an array and returns the new length of the array.


Method Description
concat() Returns new array by combining values of an array that is specified as parameter with existing array values.
every() Returns true or false if every element in the specified array satisfies a condition specified in the callback function. Returns false even if single element does not satisfy the condition.
filter() Returns a new array with all the elements that satisfy a condition specified in the callback function.
forEach() Executes a callback function for each elements of an array.
indexOf() Returns the index of the first occurrence of the specified element in the array, or -1 if it is not found.
join() Returns string of all the elements separated by the specified separator.
lastIndexOf() Returns the index of the last occurrence of the specified element in the array, or -1 if it is not found.
map() Creates a new array with the results of calling a provided function on every element in this array.
pop() Removes the last element from an array and returns that element.
push() Adds one or more elements at the end of an array and returns the new length of the array.
reduce() Pass two elements simultaneously in the callback function (till it reaches the last element) and returns a single value.
reduceRight() Pass two elements simultaneously in the callback function from right-to-left (till it reaches the last element) and returns a single value.
reverse() Reverses the elements of an array. Element at last index will be first and element at 0 index will be last.
shift() Removes the first element from an array and returns that element.
slice() Returns a new array with specified start to end elements.
some() Returns true if at least one element in this array satisfies the condition in the callback function.
sort() Sorts the elements of an array.
splice() Adds and or removes elements from an array
toString() Returns a string representing the array and its elements.
unshift() Adds one or more elements to the front of an array and returns the new length of the array.

Here are the key points of array methods

1. Array Creation

  • Array.from(): Creates an array from an array-like or iterable object.
  • Array.isArray(): Checks if a value is an array.
  • Array.of(): Creates an array from the given arguments.

2. Adding and Removing elements

  • push(): Adds elements to the end of an array.
  • pop(): Removes the last element of an array.
  • unshift(): Adds elements to the beginning of an array.
  • shift(): Removes the first element of an array.
  • splice(): Adds, removes, or replaces elements at a specific index.

3. Iteration over Elements

  • forEach(): Executes a function for each array element.

4. Transforming Arrays

  • map(): Creates a new array by transforming each element.
  • filter(): Creates a new array with elements that satisfy a condition.
  • reduce(): Accumulates array elements into a single value.
  • flat(): Flattens nested arrays up to a specified depth.
  • flatMap(): Maps and flattens the result into a single array.

5. Searching Arrays

  • find():Finds the first element that satisfiesa condition.
  • findIndex(): Finds the index of the first element that satisfies a condition.
  • includes():Checks if an array contains a specific value
  • indexOf():Finds the first occurence of a value.
  • lastIndexOf(): Finds the last occurrence of a value.

6. Sorting and reversing

  • sort(): Sorts array elements
  • reverse(): Reverses the order of elements

7. Joining and Slicing

  • concat(): Combines arrays into one.
  • slice():Extracts a section of the array.
  • join():Combines elements into a string.

8. Array Analysis

  • some(): Checks if at least one element satisfies a condition.
  • every(): Checks if all elements satisfy a condition.

9. Filling and copying

  • fill(): Fills the array with a static value.
  • copyWithin(): Copies part of an array to another location in the same array.

10. Array Properties

  • Most methods do not mutate the original array (e.g., map, filter).
  • Some methods mutate the array (e.g., push, pop, splice, sort).