javascript array contains – How to Check if an Array Contains a Value in Javascript?

Rate this post

javascript array contains – The includes() method returns true if an array contains a specified value. Example with Array contains a primitive value and Array contains an object Determine whether an array contains a value.

javascript array contains & JavaScript Array includes() Method

Checking if an Array Contains a Value in JavaScript also jQuery has a utility function $.inArray(value, array).

Check if an array contains a string

const players = ['rohit', 'virat', 'dhavan'];
const result = players.includes('rohit');

console.log(result); // true

Check if an array contains a number

const ranks = [1,2,3,4,5];

let result = ranks.includes(4); 
console.log(result); // true

result = ranks.includes(6); 
console.log(result); // false

Check if an array contains an object

const blog = {
    'name': 'blog Dev',
    'email': '[email protected]'
};
const pakainfo = {
    'name': 'pakainfo dev',
    'email': '[email protected]'
};

const list = [blog, pakainfo];
let result = list.includes(blog);

console.log(result); // true

JavaScript Array includes()

const websites = ["Infinityknow", "blogspot", "w3school", "Pakainfo"];
websites.includes("Pakainfo");

Free Live Chat for Any Issue

Start the search at position 3:

const websites = ["Infinityknow", "blogspot", "w3school", "Pakainfo"];
websites.includes("Infinityknow", 3);

check if an array includes a value in JavaScript

console.log(['w3diy', 'pakainfo', 'blogspot'].includes('pakainfo')); //true

jQuery has a utility function

$.inArray(value, array)

Don’t Miss : JavaScript InArray Check If Value Exists

Read Also:  Google Like Autosuggest Search Using PHP Ajax

JavaScript Array Contains Example

var res = Array();
if (res.contains('pakainfo')) {
   // do something special
}

Download

I hope you get an idea about javascript array contains.
I would like to have feedback on my infinityknow.com.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.