title: JavaScript declare a variable and use the comma operator in one statement?
body: <p>it's known that to declare multiple variables, one uses a format like:</p>
  
  <pre><code>let k = 0,
      j = 5 /*etc....*/
  </code></pre>
  
  <p>It's also known that to execute multiple statements in one line (which is useful for arrow functions, making it not necessary to write the <code>return</code> keyword), the comma "," operator is also used, like so:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>let r = "hello there world, how are you?"
  .split("")
  .map(x =&gt; (x+=5000, x.split("").map(
    y =&gt; y+ + 8
  ).join("")))
  .join("")
  
  console.log(r)</code></pre>
  </div>
  </div>
  </p>
  
  <p>not the most elegant example, but the point is you can execute multiple statements in one line, separated by a comma ",", and the last value is returned.</p>
  
  <p>So the question: </p>
  
  <p>how do you combine both of these techniques? Meaning, how do we declare a variable in one line, and, one comma later, use that variable for something?</p>
  
  <p>The following is not working:</p>
  
  <pre><code>let k = 0, console.log(k), k += 8
  </code></pre>
  
  <p>says</p>
  
  <blockquote>
    <p>Uncaught SyntaxError: Unexpected token '.'</p>
  </blockquote>
  
  <p>and without the console.log, it thinks I'm re-declaring k:</p>
  
  <pre><code>let k = 0, k += 8
  </code></pre>
  
  <p>gives</p>
  
  <pre><code>Uncaught SyntaxError: Identifier 'k' has already been declared
  </code></pre>
  
  <p>And putting the whole thing in parenthesis like so:</p>
  
  <pre><code>(let k = 0, k += 8);
  </code></pre>
  
  <p>gives</p>
  
  <pre><code>Uncaught SyntaxError: Unexpected identifier
  </code></pre>
  
  <p>referring to the keyword "let". However, without that keyword, there is no problem:</p>
  
  <pre><code>(k = 0, k += 8);
  </code></pre>
  
  <p>except for the fact that k now becomes a global variable, which is not wanted.</p>
  
  <p>Is there some kind of workaround here?</p>
  
  <p><strong>How can I use the comma operator together with a local variable declaration in JavaScript?</strong></p>
  
  <p>EDIT in response to VLAZ's eval part of the answer, to pass parameters into eval, a custom function can be made:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function meval(mainStr, argList) {
      let ID = (
          Math.random().toString() + 
          performance.now().toString()
          
      ).split(".").join("").split("")
      .map(x =&gt; ("qwertyuio")[x])
      .join(""),
          varName = "$______"+ID+"_____$",
          str = `
          var ${varName} = {};
          (argList =&gt; {
              Object.entries(argList).forEach(x =&gt; {
                  ${varName}[x[0]] = x[1];   
              })
               
          });
      `;
   let myEval = eval;
      
      
      
      
   return (() =&gt; {
  
    myEval(str)(argList)
    myEval(`
     ${
      Object.keys(argList).map(x =&gt; 
       "let " + x + " = " + varName + "['" + x +"'];"
      ).join("\n")
     }
     ${mainStr}
     delete window[${varName}];
    `)
    
   })()
  }
  
  meval(`
      var g = a.ko + " world!"
      
  `, {
      a: {ko: "hi"}
  })
  console.log(g);</code></pre>
  </div>
  </div>
  </p>