in Uncategorized

Array Methods in JavaScript

2 min read


Different Types of Methods

There are a few basic methods to work with arrays in JavaScript. The most basic and, perhaps the most familiar, is the .length() method.

You can get the length of the elements in an array with with this method and a for loop, which is an iterator/counter loop. For more information about for loops, check here.

An example of iterating through an array with a for loop to find the index of an element:

var array = ["Tiffany", "Cheerios", "ice cream", "pig", "Macbook"];
for (var i = 0; i < array.length; i++) {
    var index = array[i];
    console.log('Index: ', index);

This will list all the elements in the array to the console, as well as an undefined since I’m not returning a value.

forEach() Method

Another method worth noting is the forEach() method which is another iterator and perhaps more syntatic sugar than a for loop. It is worth mentioning that forEach() has perf costs and for large records and datasets, a for loop would be a better choice.

An example of a forEach on an array would look like this:

var array = [1,2,3,4,5,6,7,8,9,0];
array.forEach(function(item, index) {

This does essentially the same thing as a for loop only it is cleaner and perhaps easier to use.

map() Method

This method creates a new method from elements in an old method using a callback function with three arguments: currentValue, index, and array.

You can use map()to parse an array of objects or return a new array full of different number sets than the previous array. For instance:

var even = [2,4,6,8];
var odd = {
    return num + 1;

is something I could use to find and return a new array of odd numbers from an old array of even ones.

concat() Method

This method just concatenates two arrays into one. For instance:

    var greetArr = ["Hello", "World!"],
    jsArr = ["Javascript", "is", "fun!"];
    var jsGreet = greetArr.concat(jsArr);

This will concatenate the arrays out to look like this:
["Hello", "World!", "Javascript", "is", "fun!"]

While we don’t want the commas in between the words, this gives you a basic idea of what concat does.


These are just some of my favorite array methods for JavaScript. There are way more and you can find them in the Mozilla docs.

CC BY-SA 4.0 Array Methods in JavaScript by Tiffany White is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.