title: Javascript Ternary operator with empty else
body: <p>I'm trying to convert the following if-else to it's ternary operator representation in javascript as follows</p>
  
  <pre><code>var x = 2;
  if (x === 2) {alert("2");}
  else
           { //do nothing}
  </code></pre>
  
  <p>But when I do this:</p>
  
  <pre><code>(t==2)?(alert("1")):();
  </code></pre>
  
  <p>Chrome throws a SyntaxError.</p>
  
  <p>My question is - 
  How to have a ternary operator in javascript with an empty "else" branch - i.e the part that comes after ":".
  Also, is this allowed-  using a ternary operator in javascript to execute statements - NOT do assignment.</p>
  
  <p>Also: the above code was just a base case. I'm actually trying to get all the DOM elements of the page as an array (called all2) and then add only those elements to another array (called only) only if they have non-null class names. Here is my code: </p>
  
  <pre><code>all2.forEach(function(e){ e.getAttribute("class") ? (only.push(e.getAttribute("class"))) : (); }); 
  </code></pre>
  
  <p>If I leave the third operand blank, it throws a syntax error. Passing a null works</p>