ABOUT US SERVICES WORK TUTORIALS RESOURCES  

Nifty Javascript - Flow Control

Labeled Loops

Javascript supports statement labels and these can be referred to by the break statement. This allows code within nested loops to immediately break out of the outermost loop.

Break out of inner loop only:

var i=0

loop1:
  for (;;) {
    for (;;) {
      i++
      if (i==5) break
    }
    alert('within loop1')
    break
  }

alert('outside loop1')

Immediately break out of labeled outermost loop:

var i=0

loop1:
  for (;;) {
    for (;;) {
      i++
      if (i==5) break loop1
    }
    alert('within loop1')
  }

alert('outside loop1')


Back to Table of Contents