Skip to content

JavaScript contains array

  • by

JavaScript includes() method is used to determine whether an array contains a certain value or not and returns a boolean value indicating the result.

JavaScript contains an array example

Simple example code determines whether an array includes a certain value or not.

<!DOCTYPE html>
<html>
  <body>
    <script>
        const myArray = [1, 2, 3, 4, 5];
        console.log(myArray.includes(3));
        console.log(myArray.includes(6)); 

    </script>
  </body>
</html>

Output:

JavaScript contains array

The includes() method takes an optional second parameter, which is the index to start the search from. For example:

const myArray = [1, 2, 3, 4, 5];
console.log(myArray.includes(2, 2)); // Output: false
console.log(myArray.includes(4, 3)); // Output: true

Comment if you have any doubts or suggestions on this JS array topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

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