Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt

Q1613 How can I exit a function in Javascript?

You are here: irt.org | FAQ | JavaScript | General | Q1613 [ previous next ]

By using the return statement. For example, the following function exits as soon as the counter has reached 100:

<html>

<head>
<title>return statement</title>
</head>

<body>

<script language="JavaScript"><!--
function count() {
  for (var counter=0;;counter++) { // an infinite loop
     document.write(counter + '<br>');
     if (counter == 100) return;
  }
}

count();
//--></script>

</body>

</html>

©2013 Martin Webb