Given an Array, the task is to check whether an array contains a specific string in JavaScript and jQuery.
1. Using Array includes() Method
The includes()
method is one of the simplest ways to check if an array contains a specific string. It is designed to test for the presence of an element in an array.
let arr1 = [10, 20, 30, 40, 50]; console.log(arr1.includes(30)); // Output: true console.log(arr1.includes(70)); // Output: false let arr2 = ["HTML", "CSS", "JavaScript", "React"]; console.log(arr2.includes("CSS")); // Output: true console.log(arr2.includes("PHP")); // Output: false
2. Using Array indexOf() Method
The indexOf()
method returns the index of the first occurrence of a value in an array or -1
if the value is not found.
let arr1 = [10, 20, 30, 40, 50]; console.log(arr1.indexOf(30) !== -1); // Output: true console.log(arr1.indexOf(70) !== -1); // Output: false let arr2 = ["HTML", "CSS", "JavaScript", "React"]; console.log(arr2.indexOf("CSS") !== -1); // Output: true console.log(arr2.indexOf("PHP") !== -1); // Output: false
3. Using Array some() Method
The some()
method is useful for checking if at least one element in an array meets a specific condition. This method is particularly effective for arrays of objects or for applying custom logic.
let arr1 = [10, 20, 30, 40, 50]; let containsNum = arr1.some(item => item === 30); console.log(containsNum); // Output: true containsNum = arr1.some(item => item === 70); console.log(containsNum); // Output: false let arr2 = ["HTML", "CSS", "JavaScript", "React"]; let containsStr = arr2.some(item => item === "CSS"); console.log(containsStr); // Output: true containsStr = arr2.some(item => item === "PHP"); console.log(containsStr); // Output: false
4. Using Regular Expressions
Regular expressions (RegExp
) can be used to check for partial matches or to ignore case sensitivity.
// Applied on Numer Array let arr1 = [10, 20, 30, 40, 50]; let regex = /30/i; // Case-insensitive match let containsNum = arr1.some(item => regex.test(item)); console.log(containsNum); // Output: true regex = /70/i; containsNum = arr1.some(item => regex.test(item)); console.log(containsNum); // Output: false // Applied on String Array let arr2 = ["HTML", "CSS", "JavaScript", "React"]; regex = /CSS/i; // Case-insensitive match let containsStr = arr2.some(item => regex.test(item)); console.log(containsStr); // Output: true regex = /PHP/i; containsStr = arr2.some(item => item === "PHP"); console.log(containsStr); // Output: false */
5. Using jQuery.inArray() Method
If you’re using jQuery, the $.inArray()
function checks whether a value exists in an array.
let arr1 = [10, 20, 30, 40, 50]; console.log($.inArray(30, arr1) !== -1); // Output: true console.log($.inArray(70, arr1) !== -1); // Output: false let arr2 = ["HTML", "CSS", "JavaScript", "React"]; console.log($.inArray("CSS", arr2) !== -1); // Output: true console.log($.inArray("PHP", arr2) !== -1); // Output: false
6. Using Lodash’s _.includes() Method
If you’re using Lodash, the _.includes()
function simplifies the process of checking for the existence of a value.
let arr1 = [10, 20, 30, 40, 50]; console.log(_.includes(arr1, 30)); // Output: true console.log(_.includes(arr1, 70)); // Output: false let arr2 = ["HTML", "CSS", "JavaScript", "React"]; console.log(_.includes(arr2, "CSS")); // Output: true console.log(_.includes(arr2, "PHP")); // Output: false