/

HTML / CSS / JavaScript Tutorial

JavaScript Statement: for … in

[this page | pdf | back links]

In JavaScript, the for … in statement marks a block of statements to be executed for each element of an object (or array, although with an array it is typically better to use the for statement and to iterate over the indices of the array elements).

 

EXAMPLE:


HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head>
<style>
table,th,tr,td {border: 1px solid black; border-collapse: collapse;}
</style>
</head>
<body>
<table>
<tr>
<th>Example</th>
<th>Resulting value of <code>x</code></th>
</tr>
<tr>
<td><code id="Example"></code></td>
<td><code id="Result"></code></td>
</tr>
</table>

<script>
var s = {word1:"Hello", word2:"World"};
var x = "";
var y;
for (y in s) { x = x + s[y] };
document.getElementById("Example").innerHTML =
  'var s = {word1:"Hello"; word2:"World"};<br>' +
  'var x = "";<br>' +
  'var y;<br>' +
  'for (y in s) { x = x + s[y] };';
document.getElementById("Result").innerHTML = x;
</script>

</body>
</html>


NAVIGATION LINKS
Contents | Prev | Next | JavaScript Reserved Words


Desktop view | Switch to Mobile