Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1066612 of InternalError: too much recursion

  • Revision slug: Web/JavaScript/Reference/Errors/Too_much_recursion
  • Revision title: InternalError: too much recursion
  • Revision id: 1066612
  • Created:
  • Creator: fscholz
  • Is current revision? Yes
  • Comment new page

Revision Content

{{jsSidebar("Errors")}}

Message

InternalError: too much recursion

Error type

{{jsxref("InternalError")}}.

What went wrong?

A function that calls itself is called a recursive function. In some ways, recursion is analogous to a loop. Both execute the same code multiple times, and both require a condition (to avoid an infinite loop, or rather, infinite recursion in this case). When there is too much or infinite recursion, JavaScript will throw this error.

Examples

This recursive function runs 10 times, as per the exit condition.

function loop(x) {
  if (x >= 10) // "x >= 10" is the exit condition
    return;
  // do stuff
  loop(x + 1); // the recursive call
}
loop(0);

Setting this condition to an extremely high value, won't work:

function loop(x) {
  if (x >= 1000000000000)
    return;
  // do stuff
  loop(x + 1);
}
loop(0);

// InternalError: too much recursion

See also

Revision Source

<div>{{jsSidebar("Errors")}}</div>

<h2 id="Message">Message</h2>

<pre class="syntaxbox">
InternalError: too much recursion
</pre>

<h2 id="Error_type">Error type</h2>

<p>{{jsxref("InternalError")}}.</p>

<h2 id="What_went_wrong">What went wrong?</h2>

<p>A function that calls itself is called a <em>recursive function</em>. In some ways, recursion is analogous to a loop. Both execute the same code multiple times, and both require a condition (to avoid an infinite loop, or rather, infinite recursion in this case). When there is too much or infinite recursion, JavaScript will throw this error.</p>

<h2 id="Examples">Examples</h2>

<p>This recursive function runs 10 times, as per the exit condition.</p>

<pre class="brush: js">
function loop(x) {
  if (x &gt;= 10) // "x &gt;= 10" is the exit condition
    return;
  // do stuff
  loop(x + 1); // the recursive call
}
loop(0);</pre>

<p>Setting this condition to an extremely high value, won't work:</p>

<pre class="brush: js example-bad">
function loop(x) {
  if (x &gt;= 1000000000000)
    return;
  // do stuff
  loop(x + 1);
}
loop(0);

// InternalError: too much recursion</pre>

<h2 id="See_also">See also</h2>

<ul>
 <li>{{Glossary("Recursion")}}</li>
 <li><a href="/en-US/docs/Web/JavaScript/Guide/Functions#Recursion">Recursive functions</a></li>
</ul>
Revert to this revision