{"id":2444,"date":"2026-11-13T06:22:40","date_gmt":"2026-11-13T06:22:40","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=2444"},"modified":"2026-11-13T06:22:40","modified_gmt":"2026-11-13T06:22:40","slug":"javascript-set-add","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/javascript-set-add\/","title":{"rendered":"JavaScript Set add() Method with Examples"},"content":{"rendered":"<p>Hello friends! Once you create a <code>Set<\/code> in JavaScript, the first thing you will want to do is put some values in it. For that we use <code>add()<\/code>. The <code>add()<\/code> method inserts a value into a JavaScript <code>Set<\/code>. If the value is already there, nothing happens, because a Set never stores duplicates. <code>add()<\/code> returns the Set itself, so you can chain several calls. Let&#8217;s see this with a few simple examples.<\/p>\n<pre class=\"wp-block-code\"><code>set.add(value)<\/code><\/pre>\n<h2>Basic Example<\/h2>\n<pre class=\"wp-block-code\"><code>const tags = new Set();\ntags.add(\"js\");\ntags.add(\"css\").add(\"html\");   \/\/ chaining\ntags.add(\"js\");                \/\/ duplicate, ignored\nconsole.log(tags);\nconsole.log(tags.size);<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>Set(3) { 'js', 'css', 'html' }\n3<\/code><\/pre>\n<p>The second <code>\"js\"<\/code> is silently ignored. No error is thrown, and the size stays 3.<\/p>\n<p>So adding a duplicate is completely safe. Now let&#8217;s look at what <code>add()<\/code> gives back to us.<\/p>\n<h2>add() Returns the Same Set<\/h2>\n<pre class=\"wp-block-code\"><code>const r = tags.add(\"react\");\nconsole.log(r === tags);<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>true<\/code><\/pre>\n<p>This is why <code>tags.add(\"css\").add(\"html\")<\/code> works. Note that <code>add()<\/code> does <em>not<\/em> tell you whether the value was new. If you need to know, check with <a href=\"https:\/\/gyanipandit.com\/programming\/javascript-set-has\/\"><code>has()<\/code><\/a> first, or compare <code>size<\/code> before and after.<\/p>\n<h2>Adding Objects<\/h2>\n<p>With numbers and strings things are simple. But with objects, there is a small surprise. Sets compare objects by reference, so two objects that look the same are both added:<\/p>\n<pre class=\"wp-block-code\"><code>const people = new Set();\npeople.add({ name: \"A\" });\npeople.add({ name: \"A\" });\nconsole.log(people.size);<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>2<\/code><\/pre>\n<p>To keep objects unique by a field, store that field (such as an id) in the Set, or use a Map keyed by id.<\/p>\n<h2>Adding Values from an Array<\/h2>\n<pre class=\"wp-block-code\"><code>const nums = new Set();\n[3, 1, 3, 2, 1].forEach(n =&gt; nums.add(n));\nconsole.log([...nums]);<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>[ 3, 1, 2 ]<\/code><\/pre>\n<p>Values keep the order in which they were <em>first<\/em> added. You can get the same result in one step with <code>new Set([3, 1, 3, 2, 1])<\/code>.<\/p>\n<p>Now let&#8217;s put all of this to work in a small real-world task.<\/p>\n<h2>Practical Example: Count Unique Words<\/h2>\n<pre class=\"wp-block-code\"><code>const text = \"the cat and the hat and the bat\";\nconst words = new Set(text.split(\" \"));\nconsole.log(words.size, [...words]);<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>5 [ 'the', 'cat', 'and', 'hat', 'bat' ]<\/code><\/pre>\n<h2>Common Mistakes<\/h2>\n<p>Before we finish, keep an eye on these common mistakes:<\/p>\n<ul>\n<li><strong>Passing several values at once.<\/strong> <code>set.add(1, 2, 3)<\/code> adds only <code>1<\/code>; the extra arguments are ignored. Call <code>add()<\/code> once per value, or loop over an array.<\/li>\n<li><strong>Adding an array when you meant its items.<\/strong> <code>set.add([1, 2])<\/code> adds the array as one single value. Use <code>[1, 2].forEach(v =&gt; set.add(v))<\/code> instead.<\/li>\n<li><strong>Expecting <code>add()<\/code> to return true or false.<\/strong> It always returns the Set.<\/li>\n<\/ul>\n<p>So friends, to sum up: <code>add()<\/code> puts one value into a Set, quietly ignores duplicates without any error, keeps values in the order they were first added, and returns the Set so you can chain calls like <code>s.add(1).add(2)<\/code>. To add many values at once, loop over an array or simply create the Set from that array. Try the examples in your browser console and you will get the hang of it quickly.<\/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-has\/\">Set has()<\/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! Once you create a Set in JavaScript, the first thing you will want to do is put some values in it. For that we use add(). The add() method inserts a value into a JavaScript Set. If the value is already there, nothing happens, because a Set never stores duplicates. add() returns the &#8230; <a title=\"JavaScript Set add() Method with Examples\" class=\"read-more\" href=\"https:\/\/gyanipandit.com\/programming\/javascript-set-add\/\" aria-label=\"Read more about JavaScript Set add() Method with Examples\">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-2444","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\/2444","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=2444"}],"version-history":[{"count":3,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/2444\/revisions"}],"predecessor-version":[{"id":6134,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/2444\/revisions\/6134"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=2444"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=2444"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=2444"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}