JavaScript 30 -Day 7 Study Notes

Jeffrey Amen
2 min readNov 16, 2020
https://javascript30.com/

Day Seven of the JavaScript 30 course by Wes Bos continued looking at array methods. Array Cardio Day One was the Day 4 lesson and covered filter, map, sort, and reduce. You can see my notes from that lesson here. Today, Bos went over another group of methods; some, every, find, findIndex, and indexOf. Once again, I relied on the excellent JavaScript tutorial by W3 Schools to fill in some background information.

These are methods contained in the array object and written as array.some, array.every, array.find, array.findIndex, and array.indexOf. I will be using the shortened version from here on.

Some()

The some() method checks if any elements of an array pass a test in the form of a function. Bos used an array of objects containing people’s names and birth years. He used the some() method to check if there were people over 18 years old.

const isAdult = people.some(person =>((new Date()).getFullYear())- person.year >=19);

The return is a boolean. In the above case, it is true.

Every()

Similar to the some() method, the every() method checks if all elements of an array pass a test.

const allAdults = people.every(person =>((new Date()).getFullYear())- person.year >=19);

Also returns a boolean. But in this case, it is false.

Find()

the find() method checks the elements in the array that passes a test and returns the first incidence found. Bos used an array of objects containing text comments and their numeric references to demonstrate.

const comment = comments.find(comment => comment.id === 823423 ? true : false);

Bos used this code to find the specific occurrence of the id number 823423. The return is the specific object containing the text and numeric id.

FindIndex()

the findIndex() method also finds the first incidence of an array element that passes a test and returns the index of that element.

const index = comments.findIndex(comment => comment.id === 823423);

Similarly, the lastIndexOf() method returns the position of the last element that passes the test.

In the last exercise, Bos used the slice method and the spread operator to delete the element found. And formed a new array.

const newComments = [...comments.slice(0,index),...comments.slice(index +1)];

Compared to Day 6, this lesson was considerably easier for me.

View my study notes from Day 4, Array Cardio Part 1.

--

--

Jeffrey Amen

Retired physician, writer, aspiring website designer and developer