Best practice for testing return value of indexOf
<p>What do you normally write when you're testing for the return value of indexOf?</p>

<pre><code>if str.indexOf("a") &lt; 0 
</code></pre>

<p>vs</p>

<pre><code>if str.indexOf("a") == -1
</code></pre>

<p>Would one method be preferred over the other?</p>

<p>I'm actually posing this question for any function in any language that returns -1 on error.</p>

<p>I normally prefer the &lt; 0 approach, because if the function is extended to return -2 on some other case, the code would still work.</p>

<p>However, I notice that the == -1 approach is more commonly used. Is there a reason why?</p>
