Skip to content

JavaScript inarray check | Example code

  • by

Use includes() method to inarray check value in Javascript. This method for arrays specifically solves the problem, and so is now the preferred method.

array.includes(searchElement)

This method true if the value searchElement is found within the array.

JavaScript inarray check

Simple example code.

<!DOCTYPE html>
<html>
<body>

  <script>
    var arr = [1, 2, 3];
    res = arr.includes(2)

    console.log(arr,"has value 2", res)
  </script>

</body>
</html> 

Output:

JavaScript inarray check

More example

console.log(['joe', 'jane', 'mary'].includes('jane')); //true

You can also use Array indexOf, which is less direct, but doesn’t require polyfills for outdated browsers.

console.log(['joe', 'jane', 'mary'].indexOf('jane') >= 0); //true

Do 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 *