4

I am a beginner in javascript. I was trying recursive function in javascript.

<html>
<head>
</head>
<body>
<script type = "text/javascript">
function first(){
    document.write(" first");
    first();
}
first();
</script>
</body>
</html>

Browser prints "first" for finite number of times. Why is that? Is there any specific mechanism to stop printing "first" after certain number of time? Is it browser specific?

shriguru nayak
  • 310
  • 1
  • 3
  • 21
  • Well, you are on the right site for this... it stops because of a http://en.wikipedia.org/wiki/Stack_overflow which most browsers will flag in the console. Different browsers will stop execution at different times depending on multiple factors for example the JavaScript engine and the stack size – andyb Oct 31 '12 at 10:11

3 Answers3

4

Recursive functions aren't infinite -- they can only keep going until they run out of stack space.

'Stack space' in this context is the memory that the program (ie the browser) allocates to remembering the function call chain, so that when your functions return it knows where to return to.

That memory space is limited, and when it runs out, the program will stop and throw an error (a Stack Overflow error).

If you are using a browser that has a developer tools window (ie virtually all of the major browsers), you should be able to see the error being displayed in the console window when it happens.

The exact number of times that the browser will run your loop will vary according to the browser and how much memory it has allocated to the stack. This is not something you have any direct control over - certainly not in a browser context anyway; in lower level programming such as a C/C++ program you would have the tools to define the stack size yourself, but in a browser these things are out of your control. That said, the browser should allocate sufficient memory to the stack that a program would never be likely to hit it unless it gets into an infinite loop.

SDC
  • 14,192
  • 2
  • 35
  • 48
  • In addition to this, most browsers will simply halt the execution of Javascript if they execute for too long in order to stop the browser becoming unresponsive. – PhonicUK Oct 31 '12 at 10:09
  • Some browsers will actually ask the user if they want to terminate a "script running too slow". – Salman A Oct 31 '12 at 10:26
  • So, does that mean to say browsers are capable of printing certain number of characters. Any idea about that number? – shriguru nayak Oct 31 '12 at 10:48
  • @shrigurunayak - no, it's not to do with the number of characters, it's to do with the number of function calls you make without doing a return. There are memory limits to the number of characters you can print, but that's a completely different discussion. And in any case, the pure "number of characters" is an arbitrary question, because if you're outputting any kind of code (eg HTML, XML), the DOM tree that the browser creates from that will take far more memory than the raw characters it contains. With these kind of things, you'll see the browser slow down and crash when runs out of memory. – SDC Oct 31 '12 at 11:16
0

Check this question JavaScript Infinite Loop? and use the code in the answer to achieve the loop:

window.onload = function start() {
slide();
}
function slide() {
var num = 0, style = document.getElementById('container').style;
window.setInterval(function () {
    // increase by num 1, reset to 0 at 4
    num = (num + 1) % 4;

    // -600 * 1 = -600, -600 * 2 = -1200, etc 
    style.marginLeft = (-600 * num) + "px"; 
}, 3000); // repeat forever, polling every 3 seconds
}
Community
  • 1
  • 1
trainoasis
  • 6,419
  • 12
  • 51
  • 82
0

Recursive functions work similar in all languages. You have to write return statement, that if condition is true, return something otherwise call the function again. It all depends on requirements, how you wanna use it. This could be the algo.

function func_name(){
   if ( condition is true )
      return something
   ...
   ...
   ...
   func_name()
}
Riz
  • 9,703
  • 8
  • 38
  • 54
  • I am aware of it. But just wanted to understand how browser manipulates the number of times it should print "first". Any idea? Just curious. – shriguru nayak Oct 31 '12 at 10:11
  • That is infinite loop, will never stop. OR will wait for memory overflow. – Riz Oct 31 '12 at 10:12