How can I test if the string "Orange" without quotes, exists in a list such as the one below, using javascript?
var suggestionList = ["Apple", "Orange", "Kiwi"];
How can I test if the string "Orange" without quotes, exists in a list such as the one below, using javascript?
var suggestionList = ["Apple", "Orange", "Kiwi"];
indexOf() is the default to use in such cases.
if( suggestionList.indexOf("Orange") > -1 ) {
console.log("success");
}
.indexOf() returns the position in the array (or string) of the found element. When no elements are found it returns -1
.
Things to consider:
suggestionList.includes('Kiwi');
which will return a boolean directly. (Does not work in internet explorer, but works in IEdge)