Javascript includes() multiple values

The Javascript array includes() function will return true if a value is found in an array or false otherwise.

But is it possible to add multiple conditions to this function? Something like this:

myArray.includes("one", "two", "three")

Unfortunately the includes() function does not have a multiple values search option, but we can simulate this with the help of others Javascript array functions.

Check for at least one value to be included in an array

Let's say we want to test if AT LEAST ONE value, from a given set, is present in a Javascript array.

We can do this by mixing the includes() function and the some() function:

const myArray = ["one", "two", "three", "four", "five"]
const s1 = ["two", "seven"]
const s2 = ["zero", "ten"]

let result1 = s1.some(i => myArray.includes(i))
// result1 = true

let result2 = s2.some(i => myArray.includes(i))
// result2 = false

Check for all values to be included in an array

On the other side, if we want to check if ALL values are present in a Javascript array, we will use includes() combined with the every() method:

const myArray = ["one", "two", "three", "four", "five"]
const s1 = ["two", "four"]
const s2 = ["one", "ten"]

let result1 = s1.every(i => myArray.includes(i))
// result1 = true

let result2 = s2.every(i => myArray.includes(i))
// result2 = false

Notes on the Javascript include() function

Some final notes on the include() function:

  • it takes a second optional argument, as the index from there to start the search. For example myArray.includes("Rob", 2) will return true if the "Rob" value is found only after the index 2 of the array. The index is zero based.
  • the include function is case-sensitive. For example ["rob", "joe"].includes("Rob") will return false.

📖 50 Javascript, React and NextJs Projects

Learn by doing with this FREE ebook! Not sure what to build? Dive in with 50 projects with project briefs and wireframes! Choose from 8 project categories and get started right away.

📖 50 Javascript, React and NextJs Projects

Learn by doing with this FREE ebook! Not sure what to build? Dive in with 50 projects with project briefs and wireframes! Choose from 8 project categories and get started right away.


Leave a Reply

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

Home Screencasts Best of Newsletter Search X

📖 50 Javascript, React and NextJs Projects

Hi friend! Before you go, just wanted to let you know about the 50 Javascript, React and NextJs Projects FREE ebook.

One of the best ways to learn is by doing the work. Choose from 8 project categories and get started right away:

  • Business & Real-World
  • Games & Puzzles
  • Fun & Interesting
  • Tools & Libraries
  • Personal & Portfolio
  • Project Add-Ons
  • Productivity
  • Clones

Learn by doing with this FREE ebook! Not sure what to build? Dive in with 50 projects complete with project briefs and wireframes!

Keep building and level up! Get all projects as an ebook right to your inbox!

X