Categories
JavaScript Answers

How to search for a string inside an array of strings with JavaScript?

Spread the love

To search for a string inside an array of strings with JavaScript, we use the find method.

For instance, we write

const strs = ["abc", "def", "ghi", "jkl", "mno"];
const value = "abc";
const str = strs.find((str) => str === value);

to call strs.find with a callback that checks if str in strs is equal to value.

If there is such an entry, then the first instance of str is returned.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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