title: When to use Set in JavaScript
body: <p>I'm just a beginner in javaScript and have a background in python. I was trying this exercise to check if every character of the <em>string2</em> is contained within the <em>string1</em>.
  For example, if <em>string1</em> is &quot;hello&quot;, I'll return <code>true</code> if <em>string2</em> is &quot;leh&quot; and <code>false</code> if <em>string2</em> is &quot;low&quot;.</p>
  <p>What I came up was this:</p>
  <pre><code>function mutation(arr) {
    var set = new Set(string1.split(''));
    for (var i = 0; i &lt; string2.length; i++)
      if (!set.has(string2[i]))
        return false;
    return true;
  }
  </code></pre>
  <p>I could also go about converting string2 into a <code>Set</code> and then do take the difference i.e. the operation of <code>set(string2) - set(string1)</code> which will fetch me the set of characters that are in String2 but not in String1 but I read creating a set is costly so I didn't go ahead.</p>
  <p>I checked other solutions and everyone was using <code>string1.indexOf(letter)</code> method for checking if every letter in string2 is there in string1 or not.</p>
  <p>I want to know when should one use a <code>Set</code> difference. Why everyone is using <code>array.indexOf()</code> method which costs O(n) instead of <code>set.has()</code> method which is O(1). Are there any pitfalls if I'm using <code>set.has()</code>. (say browser compatibility)?</p>
  <p>Any recommendation is helpful.</p>