Sometimes, we want to search for a string inside an array of strings with JavaScript.
In this article, we’ll look at how to search for a string inside an array of strings with JavaScript.
How to search for a string inside an array of strings with JavaScript?
To search for a string inside an array of strings with JavaScript, we use the array find
method.
For instance, we write
const strs = ["abc", "def", "ghi", "jkl", "mno"];
const value = "abc";
const result = strs.find((str) => str === value);
to call the strs.find
method with a callback that checks is str
in strs
equals value
.
If it’s true
, the value is returned.
Conclusion
To search for a string inside an array of strings with JavaScript, we use the array find
method.