Example 1: Check Array Using includes()
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 returnstrueif the value exists in the array. - The
if...elsestatement is used to display the result as per the condition.
Example 2: Check Array Using indexOf()
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.