codewithjohn.dev
Published on

How to Check if an Array Contains a Value in JavaScript

To check if an array contains an element You can use the includes() method not only for arrays but You can also use it to check if a substring exists within a string. The array includes() method is a built-in function that checks whether an array contains a specified element or not.

The includes() method returns Boolean value if the item we are searching for is in the array or the substring is in the string it returns true if it is not the case it returns false

In this article, you'll see how to use the JavaScript includes() method to check if an element is in an Array.

includes() Syntax

The syntax of the includes() method is:

array.includes(searchElement, fromIndex)

includes() Parameters

The includes() method takes two parameters serchElement and fromIndex

  1. serchElement : The element we are searching for

  2. fromIndex : - Specifies the index from which to start the search and it is optional parameter if you do not provide the default value will be 0 which is the first index

includes() Return Value

The includes method returns Boolean value if the item we are searching for is in the array or false if it is not found

Example 1

let languages = ['JavaScript', 'python', 'java', 'typescript']

// checking whether the array contains 'JavaScript'
let check1 = languages.includes('JavaScript')

console.log(check1) // true

// checking whether the array contains 'Ruby'
let check2 = languages.includes('Ruby')

console.log(check2) // false

In the above example, we have used the includes() method to check whether the languages array contains elements JavaScript and Ruby.

The languages.includes("JavaScript") returns true since the array contains JavaScript and languages.includes("Ruby") returns false since the array does not contain Ruby.

Array.includes() is a case-sensitive method which means the method will consider the "javascript" and "JavaScript" as two different words.

Example 2

const languages = ['JavaScript', 'Python', 'Java', 'TypeScript']

// checking whether the array contains 'Python'
const check1 = languages.includes('Python')

console.log(check1) // true

// checking whether the array contains 'java'
const check2 = languages.includes('java')

console.log(check2) // false

The above program returns true for the first check and false for the second check this is because the includes() method is case sensitive. so it treats java and Java as to different strings

Example 3

Now we will also pass the second parameter to the includes() method, which tells the starting index of the search to the method:

const languages = ['Javascript', 'Python', 'Java', 'Ruby', 'C++', 'C#']

// the search starts from third last element
const check1 = languages.includes('Javascript', 2)

console.log(check1) // false

In the above program we pass a second parameter to the includes() method so that the search begins at the second index which is Java , the element JavaScript is present at the first index. So if we change the starting point of the search to 2, then the includes() method will not be able to find it. So it returns false