title: Are functions in JavaScript tail-call optimized?
body: <p>I have been trying to understand <code>Tail call optimization</code> in context of JavaScript and have written the below recursive and tail-recursive methods for <code>factorial()</code>.</p>
  
  <p><strong>Recursive:</strong></p>
  
  <pre><code>function factorial (n) {
    if (n &lt; 2) {
      return 1;
    } else {
      return n * factorial(n-1);
    }
  }
  </code></pre>
  
  <p><strong>Tail-recursive:</strong></p>
  
  <pre><code>function factorial (n) {
    function fact(n, acc) {
      if (n &lt; 2) {
        return acc;
      } else {
        return fact(n-1, n * acc);
      }
    }
  
    return fact(n, 1)
  }
  </code></pre>
  
  <p>But I am not sure if the <code>tail-recursive</code> version of the function will be optimised by JavaScript compiler as it is done in other languages like Scala etc. Can someone help me out on this one?</p>