Beginners Guide to the JavaScript Array Methods & How to Use Them


JavaScript is a powerful programming language, and as with any like it, it needs to handle many data types. To facilitate this JavaScript offers ways to store collections of data as well, such as the array data type.

This post will explain how to interact with array data using array methods. You will learn about different methods and how they work. You will also see some examples of the syntax and an explanation of the behavior. Finally, you will see some code examples of ways to use array methods in practical situations.

Let’s dive in.

Download Now: An Introduction to Java & JavaScript

JavaScript Array

JavaScript arrays are used to store data collections and are robust by design. JavaScript arrays are powerful enough to work with any type or size of data — including arrays — as values. Because of this, there must be ways to interact with them, and the more versatile, the more ways you’ll need.

This concept is where JavaScript array methods come in, allowing you to apply arrays to many tasks. Let’s talk about what methods are and why you will need them in your JavaScript software development.

What Are JavaScript Array Methods

JavaScript array methods are simple methods within the JavaScript programming language that are used on arrays to create different outcomes, handle tasks, and more. JavaScript arrays only come with three static methods, which means those methods belong to the original class. This behavior is contrary to non-static methods, which do not belong to the array class. Instead, the non-static methods are used on the instance of a class.

Let’s break this down, when you create an array, the process uses the class blueprint to create an instance of the class. An array instance is an array you create that has access to the static methods and other methods as well.

For example, one static method from the array class is called isArray(), which identifies arrays and belongs to the array class. However, the concat() method does not belong to the array class and is used on several different data types.

JavaScript Array Methods

There are a lot of JavaScript Array methods, over thirty methods that work with JavaScript Arrays. That is a lot to cover, so instead, we will go over the top fifteen most common methods programmers use in their software development. The following image is a nice little cheat sheet of the methods you will see examples of.

Image of some popular JavaScript array methods.

Now that you have an understanding of what the above methods do, it’s a good time to look at some examples of how they behave. Below is a list of examples, one example for each of the above methods.

some()

 
const ages = [3, 10, 18, 20];
ages.some(age => age > 5);
/*Would return boolean true*/

every()

 
const ages = [3, 10, 18, 20];
console.log(ages.every(age => age > 2));
/*Would return true*/

reduce()

 
const ages = [0, 1, 2, 3, 4];
ages.reduce((acc, curVal, id, arr) => acc + curVal, 1);
/*Would return 11*/

map()

 
const numbers = [1, 2, 3, 4, 5];
const doubled = numbers.map(value => value * 2);
/*Creates a new array with original values doubled*/

flat()

 
const arr4 = [1, 2, [3, 4, [5, 6, [7, 8, [9, 10]]]]]
console.log(arr4.flat(1));
/*Would return (5) [1, 2, 3, 4, [5, 6, [7, 8, [9, 10]]]]*/
console.log(arr4.flat(2));
/*Would return (7) [1, 2, 3, 4, 5, 6, [7, 8, [9, 10]]]*/
console.log(arr4.flat(Infinity));
/*Would return (10) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]*/

filter(): 

 
const array = [-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
console.log(array.filter(num => num > 0 && num <= 10));
/*Would return (10) [1,2,3,4,5,6,7,8,9,10]*/

forEach()

 
const primeNums = [2,3,5,7,11,13];
array.forEach((item) => console.log(item));
/*Would print each array item to the console*/

findIndex()

 
const nums = [4, 6, 8, 9, 12];
nums.findIndex( num => num == 4));
/*Would return 0 which is the index of the item with value 4*/

find()

 
const nums = [1, 2, 3, 4]
console.log(nums.find( num => num == 3 ))
/*Would return the matching value 3, no match returns undefined*/

sort()

 
const nums = [4, 2, 5, 1, 3]
nums.sort((a, b) => a - b)
/*Would return original array modified with values sorted*/

concat()

 
const alpha = ['a', 'b', 'c'];
const numeric = [1, 2, 3];
const alphaNumeric = alpha.concat(numeric);
console.log(alphaNumeric)

fill()

 
const original = [1, 2, 3]
const result = original.fill(4)
/*Would modify array, and return with all values changed to provided value*/

includes()

 
const nums = [1, 2, 3];
console.log(nums.includes(2));
/*Would return true*/
console.log(nums.includes(4));
/*Would return false*/

reverse()

 
const nums = [1, 2, 3];
console.log(nums.reverse());
/*Would return array with values in reverse order*/

flatMap()

 
const array = [[1], [2], [3], [4], [5]]
const a = array.flatMap(arr => arr * 10)
/*With .flat() and .map(), identical in behavior to the above*/
const b = array.flat().map(arr => arr * 10)
/*Both would return [10, 20, 30, 40, 50]*/

Getting Started With JavaScript Array Methods

You’ve learned a lot about what JavaScript array methods are and how they work. You’ve learned about the syntax, and the structure of different methods. Following that you saw a cheat sheet of the fifteen most popular methods for arrays. Finally you saw each of the methods used in examples to help drive the concepts home.

Moving forward you can use that cheat sheet as a reference point to practice different tasks with, you can also add to it. A great way to keep track of those neat little tricks and tips is to create cheat sheets for different needs. Practice and explore and you will begin committing things to memory without even knowing it.

java




blog.hubspot.com

Leave a Reply

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