Should I test if an element exist before doing anything to it?
<p>Let's say I had the following HTML and Javascript.  Here, there is only 1 div on the page, but at any given time, there could be a total of 2 divs (dynamically inserted).  </p>

<p>In jQuery, should you first test to see if an element exists before doing anything to it, especially if you know there's a chance it might not be present?  </p>

<p>Should I test it with <code>if (('#div2).length &gt; 0)</code>?  </p>

<p>Or should I just let jQuery figure it out?  I know if doesn't exist, jQuery is smart enough to know the element isn't there, so it won't do anything to it nor would it throw an error.  But is it better to first test to see if it exist?  Best practice maybe or is there a performance difference?</p>

<p><strong>HTML:</strong></p>

<pre><code>&lt;div id="div1"&gt;&lt;/div&gt;
</code></pre>

<p><strong>Javascript:</strong></p>

<pre><code>$('#div1').text('Hello World!');

/*
  Should I first test and see if this div exists?
  With if (('#div2').length &gt; 0)
  Or leave it as is and let jQuery handle it
*/
$('#div2').text('Should I have first tested to see if this div existed?');
</code></pre>
