{"id":336,"date":"2020-08-19T18:19:39","date_gmt":"2020-08-19T18:19:39","guid":{"rendered":"https:\/\/datatype.co.in\/blog\/?p=336"},"modified":"2020-08-26T09:49:13","modified_gmt":"2020-08-26T09:49:13","slug":"how-to-check-if-a-value-exists-in-an-array-in-javascript","status":"publish","type":"post","link":"https:\/\/datatype.co.in\/blog\/how-to-check-if-a-value-exists-in-an-array-in-javascript\/","title":{"rendered":"How to Check If a Value Exists in an Array in JavaScript"},"content":{"rendered":"\n<p> You can use the&nbsp;<code>indexOf()<\/code>&nbsp;method to check if a given element exists in an array or not. The&nbsp;<code>indexOf()<\/code>&nbsp;method returns the index of the element inside the array if it is found, and returns -1 if it not found. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var colors = [\"Red\", \"Blue\", \"Orange\", \"Black\"];\n\/\/ Check if an element exist\nif(colors.indexOf(\"Black\") !== -1){\n   \/\/ Black exists \n}else{\n   \/\/ Black does not exist\n}\n\/\/ Similarly you can try\ncolors.indexOf(\"Yellow\") !== -1 \/\/ false\ncolors.indexOf(\"Red\") !== -1 \/\/ true\n<\/code><\/pre>\n\n\n\n<p>You can define a utility function for this purpose and reuse it whenever needed.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function elementExist(arr, element){\n  if(arr.indexOf(element) !== -1){\n    return true;\n  }else{\n    return false;\n  }\n}\n\/\/ Test the function\nvar colors = [\"Red\", \"Blue\", \"Orange\", \"Black\"];\nelementExist(colors, \"Red\") \/\/ true\nelementExist(colors, \"Purple\") \/\/ false\n<\/code><\/pre>\n\n\n\n<p>ES6 has introduced the&nbsp;<code>includes()<\/code>&nbsp;method to perform this task very easily. This method returns only&nbsp;<code>true<\/code>&nbsp;or&nbsp;<code>false<\/code>&nbsp;instead of index number. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var colors = [\"Red\", \"Blue\", \"Orange\", \"Black\"];\ncolors.includes(\"Red\") \/\/ true\ncolors.includes(\"Purple\") \/\/ false<\/code><\/pre>\n\n\n\n<p>\n\nPlease note that <code>includes()<\/code> method does not work in Internet Explorer (IE) browser. \n\n<\/p>\n\n\n\n<p><a href=\"https:\/\/datatype.co.in\/code-editor\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"Try yourself (opens in a new tab)\">Try yourself<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can use the&nbsp;indexOf()&nbsp;method to check if a given element exists in an array or not. The&nbsp;indexOf()&nbsp;method returns the index of the element inside the array if it is found,&nbsp;[ &hellip; ]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[150,151,89,100],"class_list":["post-336","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-check-if-an-element-exists-in-an-array","tag-check-if-array-contains-a-value-in-javascript","tag-js-indexof-example","tag-js-interview-question","list-style-post"],"_links":{"self":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/336","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/comments?post=336"}],"version-history":[{"count":10,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/336\/revisions"}],"predecessor-version":[{"id":412,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/336\/revisions\/412"}],"wp:attachment":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/media?parent=336"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/categories?post=336"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/tags?post=336"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}