Skip to content

JavaScript if in array | Example code

  • by

Use JavaScript includes() method to check if the value in Array. This method returns true if an array contains a specified value otherwise returns false if the value is not found.

array.includes(element, start)

Note: The includes() method is case sensitive.

JavaScript if in array

Simple example code check if string is in array js.

<!DOCTYPE html>


    var colors = ["red", "blue", "green"];
    var hasRed = colors.includes("red"); 
    var hasYellow = colors.includes("yellow"); 





JavaScript if in array

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

Many frameworks also offer similar methods:

Note: that some frameworks implement this as a function, while others add the function to the array prototype.

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 *