title: Return string from function (Without Eval)
body: <p>I have this function that generates a random string!</p>
  
  <pre><code>function randString() {
    var char = "0123456789abcdefghijklmnopqrstuvwxyz",
        fullchar = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
        genHash  = "",
        i;
  
    for (i = 0; i &lt; 8; i++) {
      var rnum = Math.floor(Math.random() * char.length)
      return genHash += char.substring(rnum, rnum + 1)
    }
  }
  </code></pre>
  
  <p>What I'm trying to do is when I click a button I want my <code>genHash</code> to be various class names.</p>
  
  <pre><code>$("[data-action=newlist]").on("click", function() {
    randString()
  
    var newBtnList = $("&lt;em&gt;", {
      text: genHash,
      title: genHash,
      class: "new-"+ genHash
    }).appendTo("[data-action=list-tree]").on("click", function() {
      // Show active editor
      $("." + genHash).removeClass("hide")
    })
  })
  </code></pre>
  
  <p>I know I can do it inside the <code>randString()</code> function (such as set it as an input's value and grab the value back from the input), but what I'm trying to figure out is how can I to it outside of the function, thus return the string from the function as states in my title. </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function randString() {
    var char = "0123456789abcdefghijklmnopqrstuvwxyz",
        fullchar = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
        genHash  = "",
        i;
  
    for (i = 0; i &lt; 8; i++) {
      var rnum = Math.floor(Math.random() * char.length)
      return genHash += char.substring(rnum, rnum + 1)
    }
  }
  
  // Create a new list
  $("[data-action=newlist]").on("click", function() {
    randString()
  
    var newBtnList = $("&lt;em&gt;", {
      text: genHash,
      title: genHash,
      class: "new-"+ genHash
    }).appendTo("[data-action=list-tree]").on("click", function() {
      // Show active editor
      $("." + genHash).removeClass("hide")
    })
  })</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;
  &lt;button type="button" data-action="newlist"&gt;
    New
  &lt;/button&gt;
  &lt;div data-action="list-tree"&gt;&lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>