javascript check if DOM element exists best practice
<p>What is the best practice to check if a DOM element exists in javascript?</p>

<p>Should one check if an item exists prior to using it, like so?</p>

<pre><code>if ($("#" + machineId + packageId.removeSpecialChars().toUpperCase() + "").size() != 0) {
  var row = $("#" + machineId + packageId.removeSpecialChars().toUpperCase() + "");
}
</code></pre>

<p>wouldn't this execute <code>packageId.removeSpecialChars().toUpperCase()</code> twice?</p>

<p>OR would this be better option?</p>

<pre><code>var row = $("#" + machineId + packageId.removeSpecialChars().toUpperCase() + ""); 
if (row)
{
  // do something
}
</code></pre>

<p>However, wouldn't it throw an exception when not found?</p>
