Fun “duh” Mentals: Array.some()

Brad Hankee
Code a la Carte
Published in
2 min readOct 12, 2017

Fun as in Fun, duh as in I should know this, Mentals as in “Remember this!”

The more I delve into podcasts, books, tutorials etc what do you think the one thing I here from seasoned programmers is:

a) Get good at the newest JS framework

b) Use words online to fight about the best code editor

c) Learn the basics very very well, and then learn them better.

If you said “b”, then you’re close, but the answer is “c”. Learning the basics is important. I think it would be awesome to be able to do just about anything in vanilla JavaScript and then be able to use anything abstracted on top to make it easier, more readable and faster. So today begins the journaling of this personal journey to get better at the easy stuff, you know the stuff that often stomps us all since we feel we know it so well. Better at learning the basic JS methods. Better at working with the DOM. Better at knowing CSS. Better at becoming a better developer. Let’s do this starting with a underutilized method on the Array object… Array.some().

var goodFood = [‘pizza’, ’tacos’, ’more tacos’];

function eat(arr, food) {
return arr.some(val => {
return food === val;
}) ;
};

console.log(eat(goodFood, “tacos”));

This would return True due to the array goodFood having at least one instance of “tacos”.

USE CASE: If you need to look into an array to see if an item, or items, are there and get back a boolean. Instead of interating through the array Array.some() is good for this.

TAKE-AWAYS: Although there are many ways to often get to the same result it seems that Array.some() is straight forward and would be fairly efficient in terms of Big O.

This is one to use next time your playing with some arrays!

Check out my site.

Follow me on Twitter.

— Brad

--

--

Brad Hankee
Code a la Carte

Full stack developer / foodie that writes about daily learnings.