Core JavaScript

For-In Loops

A for-in loop is a variant of a for loop that cycles over the indices of an array. This allows programmers to conveniently access each element in an array. Below, we have a simple example of a JavaScript for-in loop in the file "ForInLoops.js". The html file "ForInLoops.html" calls this JavaScript code. The loop prints out each of the elements of the array, which is shown in the box labeled "Output" below the code.

ForInLoop.html

<!DOCTYPE html>
<html>
<head>
    <title>XoaX.net's Javascript</title>
</head>
<body>
    <script type="text/javascript" src="ForInLoop.js"></script>
</body>
</html>

ForInLoop.js

var iaA = [2,3,6,9,2];
// This loops over the entries in the array iaA by setting a to the value of the index.
for(var a in iaA){
	document.writeln("iaA["+ a + "] = " + iaA[a] + "<br />");
}
 

Output

 
 

© 2007–2024 XoaX.net LLC. All rights reserved.