title: Is it wrong too return different types in a javascript function?
body: <p>Is it wrong to return two different types from a JavaScript function? This is often the case in PHP. For instance PHP's <code>strpos()</code>-function returns an integer telling you the position or false if not found. In Java this would not be possible since it's not a loose typed language and the return type is defined with the function.</p>
  
  <p>So would be be wrong to do the same in JavaScript?</p>
  
  <p>E.g.</p>
  
  <pre><code>function num(x, y) {
    if (x !== y) {return false;}
    if (x &lt; 5) {return 5;}
    return x;
  }
  </code></pre>
  
  <p>In above example we return an integer or boolean. But is it wrong? Would it make the life of the JavaScript engine harder and perhaps force it to de-optimize?</p>
  
  <p>Or in another example, what if I have a function that creates an element and tries to put it into the DOM then returns the newly created element object. But if it can't find the parent element you send as an param it return false.</p>