Check If A String Is Contained In An Array In JavaScript

Check if a String is contained in an Array in JavaScript

If you want to check if a string is contained in an array in JavaScript, you can use some built-in methods in JavaScript include indexOf() includes() join() and search() methods. Let’s find out what those methods are through some examples below.

How to check if a string is contained in an array in JavaScript?

I would follow two directions as follows:

  • Check directly.
  • Bring the array to the handle and then search.

Check directly 

indexOf() method

Syntax:

array.indexOf(element, start)

Parameters:

  • element: This is any string.
  • start: This is where the function starts looking.

Example:

var fruits = ["Lemon", "Orange", "Apple", "Cherry"];

// Check directly 
if (fruits.indexOf("Apple") !== -1) {
	console.log("fruits have Apple");
} else {
	console.log("fruits have not Apple");
}

Output:

fruits have Apple

In the above example, we can see that I compared the return result of the indexOf() method with -1 because after using the indexOf() method, it will return the address of the string. The return value will be -1, so I compare the result of the method with -1.

includes() method

Syntax:

array.includes(element, start)

Parameters:

  • element: This is any string.
  • start: This is where the function starts looking.

Example:

var fruits = ["Lemon", "Orange", "Apple", "Cherry"];

// Check directly 
if (fruits.includes("Apple")) {
	console.log("fruits have Apple");
} else {
	console.log("fruits have not Apple");
}

Output:

fruits have Apple

Unlike the indexOf() method, it will return a boolean value, so we use it as a condition for the if statement.

Return the array to the string and then search

In this method, we need to return the array to a string. I will use two methods, join() and toString().

join() method

Syntax:

array.join(separator)

Parameter:

  • separator: A substring used to concatenate array elements.

Example:

var fruits = ["Lemon", "Orange", "Apple", "Cherry"];

// Convert array to string
var fruitsStr = fruits.join(", ");
console.log(fruitsStr);

Output:

Lemon, Orange, Apple, Cherry

toString() method

Syntax:

array.toString()

Example:

var fruits = ["Lemon", "Orange", "Apple", "Cherry"];

// Convert array to string
var fruitsStr = fruits.toString();
console.log(fruitsStr);

Output:

Lemon, Orange, Apple, Cherry

After returning the array to the string, I will search the string with the search() method or any string search method.

search() method

Syntax:

string.search(searchValue)

Parameter:

  • searchValue: is the string the value you want to search.

Example:

var fruits = ["Lemon", "Orange", "Apple", "Cherry"];

// Convert array to string
var fruitsStr = fruits.toString();

// Check-in array 
if (fruitsStr.search("Apple") !== -1) {
	console.log("fruits have Apple");
} else {
	console.log("fruits have not Apple");
}

Output:

fruits have Apple

search() method returns the position of the passed parameter value and will return -1 when the passed parameter is not found. So, in the above example, I compared the return value of the search() method with -1 to see if the string to check is present.

Summary

I have shown you some methods to check if a string is contained in an array in JavaScript. Try them out. I think they will help you and your program. Good luck!

Maybe you are interested:

Leave a Reply

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