Asked 7 years ago
17 Jan 2017
Views 1113
lain

lain posted

function overflow in JavaScript

how to prevent function overflow in JavaScript ?


function recursion(){
return recursion();
}
recursion();


when run code for recursion i got error like
RangeError :: Maximum Call stack size exceeded

so it is function overflow i think

so what is prevention of the function overflow ? and how can i avoid RangeError :: Maximum Call stack size exceeded ?
Rasi

Rasi
answered Nov 30 '-1 00:00

if you using recursion than you need to choose one end of recursion . above code clearly says that it never end .

so try to use if or other condition statement to break the recursion


var d=0;
function increment(){
 if(d==100){
  return d;
 }else {
  d++;
  return increment();
 }
} 
increment();

above code will stop recursion when it reach at 100 . recursion without end is useless .

its not function overflow , but misuse of recursion.
Post Answer