Tutorials

Learn web design and programming with our free video and text tutorials.

Javascript for in Statement Tutorial

The for...in statement is used to loop (iterate) through the elements of an array or through the properties of an object.

The javascript code below loops through the elements of an array.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>WebDevelopmentTutorials.com</title>

</head>
<body>

<script type="text/javascript" language="javascript">

var x;
var jobs = new Array();
jobs[0] = "entry level";
jobs[1] = "management";
jobs[2] = "ceo";

for (x in jobs)
{
document.write(jobs[x] + "<br />");
}

</script>

</body>
</html>

The javascript code above displays: