Skip to content

Array methods in JavaScript | Code

  • by

JavaScript Array provides a lot of methods. JavaScript array is an object that represents a collection of similar types of elements. Let’s see the list of JavaScript array methods with their description.

MethodsDescription
concat()It returns a new array object that contains two or more merged arrays.
copywithin()It copies the part of the given array with its own elements and returns the modified array.
entries()It creates an iterator object and a loop that iterates over each key/value pair.
every()It determines whether all an array’s elements satisfy the provided function conditions.
flat()It creates a new array carrying sub-array elements concatenated recursively till the specified depth.
flatMap()It maps all array elements via a mapping function, then flattens the result into a new array.
fill()It fills elements into an array with static values.
from()It creates a new array carrying the exact copy of another array element.
filter()It returns the new array containing the elements that pass the provided function conditions.
find()It returns the value of the first element in the given array that satisfies the specified condition.
findIndex()It returns the index value of the first element in the given array that satisfies the specified condition.
forEach()It invokes the provided function once for each element of an array.
includes()It checks whether the given array contains the specified element.
indexOf()It searches the specified element in the given array and returns the index of the first match.
isArray()It tests if the passed value is an array.
join()It joins the elements of an array as a string.
keys()It creates an iterator object that contains only the array’s keys, then loops through these keys.
lastIndexOf()It searches the specified element in the given array and returns the index of the last match.
map()It calls the specified function for every array element and returns the new array
of()It creates a new array from a variable number of arguments, holding any type of argument.
pop()It removes and returns the last element of an array.
push()It adds one or more elements to the end of an array.
reverse()It reverses the elements of the given array.
reduce(function, initial)It executes a provided function for each value from left to right and reduces the array to a single value.
reduceRight()It executes a provided function for each value from right to left and reduces the array to a single value.
some()It determines if any element of the array passes the test of the implemented function.
shift()It removes and returns the first element of an array.
slice()It returns a new array containing the copy of the part of the given array.
sort()It returns the element of the given array in sorted order.
splice()It adds/removes elements to/from the given array.
toLocaleString()It returns a string containing all the elements of a specified array.
toString()It converts the elements of a specified array into string form, without affecting the original array.
unshift()It adds one or more elements at the beginning of the given array.
values()It creates a new iterator object carrying values for each index in the array.

Array methods in JavaScript

Simple example code.

<!DOCTYPE html>
<html>
<body>
  <script>
    let arr = ["A", "B", 1, 2, 3, 5];
    console.log(arr.pop(), arr);

    console.log(arr.push(22),arr);

    console.log(arr.shift(),arr);

    console.log(arr.unshift(),arr);

    console.log(arr.splice(3));

  </script>

</body>
</html>

Output:

Array methods in JavaScript

Iterate: forEach

["Bilbo", "Gandalf", "Nazgul"].forEach((item, index, array) => {
  alert(`${item} is at index ${index} in ${array}`);
});

JavaScript Array length

const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits[fruits.length] = "Kiwi";

Merging (Concatenating) Arrays

const myGirls = ["Cecilie", "Lone"];
const myBoys = ["Emil", "Tobias", "Linus"];

const myChildren = myGirls.concat(myBoys);

Do comment if you have any doubts or suggestions on these JS methods examples.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

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