How to check Array contains an Item in JavaScript?

Hi,
 
Here is the JavaScript  code to check Array contains an item in JavaScript.
 
var vArrValues = [“1”, “2”, “3”, “4”];

// Check Array Contains Values
Array.prototype.contains = function (element) {
     return this.indexOf(element) > -1;
};

function CheckArrayContaintsValue(vValue) {

    if (vArrValues.contains(vValue)) {
            return true;
    }
    else {
       return false;
     }
}

CheckArrayContaintsValue(“1”); // Returns True
CheckArrayContaintsValue(“5”); // Returns False
 
Hope this helps.
 

Happy Coding.
Gopinath

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s