JavaScript Program to Check if An Array Contains a Specified Value

To understand this example, you should have the knowledge of the following JavaScript programming topics:


Example 1: Check Array Using includes()

// program to check if an array contains a specified value

const array = ['you', 'will', 'learn', 'javascript'];

const hasValue = array.includes('javascript');

// check the condition
if(hasValue) {
    console.log('Array contains a value.');
} else {
    console.log('Array does not contain a value.');
}

Output

Array contains a value.

In the above program, the includes() method is used to check if an array contains a specified value.

  • The includes() method returns true if the value exists in the array.
  • The if...else statement is used to display the result as per the condition.

Example 2: Check Array Using indexOf()

// program to check if an array contains a specified value

const array = ['you', 'will', 'learn', 'javascript'];

const hasValue = array.indexOf('javascript') !== -1;

// check the condition
if(hasValue) {
    console.log('Array contains a value.');
} else {
    console.log('Array does not contain a value.');
}

Output

Array contains a value.

In the above program, the indexOf() method is used with the if...else statement to check if an array contains a specified value.

The indexOf() method searches an array and returns the position of the first occurrence. If the value cannot be found, it returns -1.

Note: Both includes() and indexOf() are case sensitive. Hence, J and j are different.

Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges