Arrays in JavaScript — Arrays[1]

Elijah Samuels
4 min readAug 6, 2021

--

Here’s round 2! Or would you say, array[1] for the second item in the list? Ok, ok, ok… I promise to keep the puns to a !max. Oh god, I did it again.

So, for whatever reason, I find it easier to mentally group together certain methods. So those first methods (pop, slice, splice, shift, unshift) I think of as one group. I guess this is because I think of them as working with one item in an array at a time. Yes, some of the can do multiple items at a time. This is just how I keep track of them all.

So for group two, we’ll think of these as the boolean methods. Fortunately, it’s a short list! So to start things off, let’s make a couple arrays.

Sample arrays to work with

.includes()

Determines whether the array contains a value, returning true or false as appropriate.

The includes() method will check for exact equality after doing any conversion.

Cool, but what do you mean exactly?

To break it all down, testArray1 has a list of items that are numbers. Meaning their data type is a Number primitive as opposed to a String. testArray2 has a list of words (strings) that spell out numbers.

So .includes() asks the array it’s chained to whatever it has as an argument whether it is in the array or not. If it’s there, it’ll return true and if not, it’ll return false. Does testArray1 have 1 in it? Yes. Does testArray1 have “1” in it? No.

A simple, but kind of fun thing to do is do some logic inside this method. Does testArray1 have the sum of 1+1? Yes. What if we convert a string that is a number to a number? Does testArray1 have the sum of parseInt(“1+1”)? Yes!

One of my personal favorites is decoding numbers to letters. I guess it reminds me of old spy films. So character codes for 115, 105, 120 are “s”, “i”, “x”. Making a string with the String object and basing that string off of the .fromCharCode() method gives us a word, in this case, “six”. Does testArray2 include “six”? Yes!

.every()

yourArrayName.every( element => {return yourFunction})

Returns true if every element in this array satisfies the testing function.

This method will check every element in the array against whatever statement you check it against. So we could check to see if every element in the array is greater than zero, or if it’s a number or something else. I’m a fan of the ES6 arrow functions, so these are my examples.

In the first example, we’re taking testArray1, and checking to see (returning) if every element is great than 0. It is, so it returns true.

Is every element greater than 1? No, because 1 isn’t greater than 1, so it returns false.

But is every element equal to or greater than 1? Yes, and the return is true.

If, for some reason, we had a mix of data types in our array, and we needed to check this, we can! The “typeof” key word allows us to check the data type of an object. In testArray1, we’re checking the typeof every element to see if it is exactly equal to “number”. Every element is exactly equal to the ‘number’ type, so it returns true. Awesome, and nothing is mixed up.

What if we did have an array with some different value types?!

Here are a couple ways to join our arrays. Not to get off topic, but we have the .concat() method for arrays, and the spread operator (…). Yup, just three dots “…”

The first two examples return false because every element is not only a number or only a string. The third example returns true because every element is a string OR number.

.some()

Returns true if at least one element in this array satisfies the provided testing function.

Ok, so this is really similar to .every() except it only cares if at least one element meets criteria.

--

--

No responses yet