{"id":2442,"date":"2026-11-12T11:58:07","date_gmt":"2026-11-12T11:58:07","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=2442"},"modified":"2026-11-12T11:58:31","modified_gmt":"2026-11-12T11:58:31","slug":"javascript-set-has","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/javascript-set-has\/","title":{"rendered":"JavaScript Set has() Method: Check if a Value Exists"},"content":{"rendered":"<p>Hello friends! Very often in a program we need to ask a simple question: &#8220;is this item already in my list?&#8221; If the list is a JavaScript <code>Set<\/code>, the answer comes from <code>has()<\/code>. The <code>has()<\/code> method of a JavaScript <code>Set<\/code> checks whether a value is present in the set. It returns <code>true<\/code> or <code>false<\/code> and never changes the set.<\/p>\n<pre class=\"wp-block-code\"><code>set.has(value)<\/code><\/pre>\n<p>It is the Set&#8217;s answer to &#8220;is this item already here?&#8221;, and it is much faster than searching an array when you have many items. Let&#8217;s see how it works.<\/p>\n<h2>Basic Example<\/h2>\n<pre class=\"wp-block-code\"><code>const fruits = new Set([\"apple\", \"mango\", \"banana\"]);\nconsole.log(fruits.has(\"mango\"));\nconsole.log(fruits.has(\"Mango\"));\nconsole.log(fruits.has(\"grapes\"));<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>true\nfalse\nfalse<\/code><\/pre>\n<p>The check is exact, so <code>\"Mango\"<\/code> with a capital M is a different value from <code>\"mango\"<\/code>. If case should not matter in your program, store lower-case values and check with <code>value.toLowerCase()<\/code>.<\/p>\n<p>But what exactly does &#8220;exact&#8221; mean here? That is the next thing to understand.<\/p>\n<h2>How has() Compares Values<\/h2>\n<p><code>has()<\/code> uses a rule called <em>SameValueZero<\/em>. It works like <code>===<\/code>, with one useful difference: <code>NaN<\/code> is treated as equal to <code>NaN<\/code>. Also, <code>0<\/code> and <code>-0<\/code> count as the same value.<\/p>\n<pre class=\"wp-block-code\"><code>const s = new Set([1, \"2\", NaN, 0]);\nconsole.log(s.has(1), s.has(\"1\"), s.has(2), s.has(\"2\"));\nconsole.log(s.has(NaN));\nconsole.log(s.has(-0));<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>true false false true\ntrue\ntrue<\/code><\/pre>\n<p>There is no type conversion: the number <code>1<\/code> and the string <code>\"1\"<\/code> are different values.<\/p>\n<h2>has() with Objects<\/h2>\n<p>This is where many beginners get confused. Objects are compared by <strong>reference<\/strong>, not by their contents. A new object that looks the same is still a different object:<\/p>\n<pre class=\"wp-block-code\"><code>const user = { name: \"Riya\" };\nconst users = new Set([user]);\nconsole.log(users.has(user));\nconsole.log(users.has({ name: \"Riya\" }));<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>true\nfalse<\/code><\/pre>\n<p>If you need to look up objects by their content, store an id in the set instead (for example <code>new Set([user.id])<\/code>), or use a Map keyed by id.<\/p>\n<h2>Practical Example: Skipping Duplicates<\/h2>\n<p>Now let&#8217;s use <code>has()<\/code> in a small real-world task: keeping track of pages a user has already visited.<\/p>\n<pre class=\"wp-block-code\"><code>const visited = new Set();\nfor (const page of [\"home\", \"about\", \"home\", \"contact\", \"about\"]) {\n  if (visited.has(page)) {\n    console.log(\"already seen:\", page);\n  } else {\n    visited.add(page);\n  }\n}\nconsole.log(visited.size);<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>already seen: home\nalready seen: about\n3<\/code><\/pre>\n<h2>Set.has() vs Array.includes()<\/h2>\n<p>You might be thinking, arrays also have <code>includes()<\/code>, so why use a Set at all? Here is a quick comparison:<\/p>\n<table>\n<thead>\n<tr>\n<th><\/th>\n<th><code>Set.has()<\/code><\/th>\n<th><code>Array.includes()<\/code><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Speed on large data<\/td>\n<td>Fast, roughly constant time<\/td>\n<td>Slower, checks items one by one<\/td>\n<\/tr>\n<tr>\n<td>Finds <code>NaN<\/code><\/td>\n<td>Yes<\/td>\n<td>Yes (but <code>indexOf<\/code> does not)<\/td>\n<\/tr>\n<tr>\n<td>Duplicates allowed<\/td>\n<td>No<\/td>\n<td>Yes<\/td>\n<\/tr>\n<tr>\n<td>Best for<\/td>\n<td>Repeated membership checks<\/td>\n<td>Small lists, or when order and duplicates matter<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<pre class=\"wp-block-code\"><code>const arr = [\"a\", \"b\", NaN];\nconsole.log(arr.includes(NaN), arr.indexOf(NaN));<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>true -1<\/code><\/pre>\n<p>If your code checks the same list again and again (inside a loop, for example), build a Set once and use <code>has()<\/code>.<\/p>\n<p>So friends, remember this: <code>has()<\/code> always returns a simple <code>true<\/code> or <code>false<\/code>, it matches values exactly, and it checks objects by reference. Use it whenever your code needs to ask &#8220;have I seen this before?&#8221; again and again.<\/p>\n<p>Related: <a href=\"https:\/\/gyanipandit.com\/programming\/sets-in-javascript\/\">Sets in JavaScript<\/a> \u00b7 <a href=\"https:\/\/gyanipandit.com\/programming\/javascript-set-add\/\">Set add()<\/a> \u00b7 <a href=\"https:\/\/gyanipandit.com\/programming\/javascript-set-delete\/\">Set delete()<\/a> \u00b7 <a href=\"https:\/\/gyanipandit.com\/programming\/set-size-property-javascript\/\">Set size<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello friends! Very often in a program we need to ask a simple question: &#8220;is this item already in my list?&#8221; If the list is a JavaScript Set, the answer comes from has(). The has() method of a JavaScript Set checks whether a value is present in the set. It returns true or false and &#8230; <a title=\"JavaScript Set has() Method: Check if a Value Exists\" class=\"read-more\" href=\"https:\/\/gyanipandit.com\/programming\/javascript-set-has\/\" aria-label=\"Read more about JavaScript Set has() Method: Check if a Value Exists\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[14,15],"class_list":["post-2442","post","type-post","status-publish","format-standard","hentry","category-javascript-tutorial","tag-javascript","tag-javascript-tutorial"],"_links":{"self":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/2442","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/comments?post=2442"}],"version-history":[{"count":5,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/2442\/revisions"}],"predecessor-version":[{"id":6133,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/2442\/revisions\/6133"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=2442"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=2442"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=2442"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}