JavaScript Array .some() method

The JavaScript array .some() method is used to check whether at least one desired item exists among an array of items. It will return true if it does, false if not.

This array.some() built-in JavaScript function accepts a callback function that we can use to get every item on each iteration of the array.

The callback function contains the current iterated item, the current index and the copy of the array itself on each iteration.

Array .some() Method Syntax

This is how you can use the the .some() method.


  array.some((item, index, array) => {
    // run your test here and return a boolean value: true or false
  })

Array .some() Method on Array of Objects


  const ninjas = [
    { name: "Uzumaki Naruto", gender: "male", fruit: "🍍" },
    { name: "Haruno Sakura", gender: "female", fruit: "🍌" },
    { name: "Uchiha Sasuke", gender: "male", fruit: "🍉" },
  ];

  const hasFemale = ninjas.some((ninja) => {
    return ninja.gender === "female";
  });

  console.log(hasFemale);
  // the output would be "true"

Based on the given data above, I have an array of objects stored on a variable named ninjas. It contains the name, gender and a favorite fruit of the ninja.

What I want to do is to check whether there's a female among the group. We can achive this using .some() method. The output of our code on the console would be: true

Watch Tutorial

Here's a quick video tutorial about the JavaScript array .some() method.