JavaScript for in loop

The for-in loop is used to loop through an object’s properties. I have not posted any article on Objects yet, so you may not feel comfortable with this loop. But once you got the knowledge of objects in JavaScript, you will find this loop very useful with JavaScript Objects.

Syntax

for (variable_name in object) {
statement or block to execute
}

In each time when loop iterate one property from object is assigned to variable_name and this loop continues till all the properties of the object are exhausted.

Example:

<script>
var string = “”;
var objectPerson = {name: “Albert”, address: “202/A”, age: 30};
for (var value in objectPerson) {
string += objectPerson[value] + ” “;
}
document.write(string);
</script>


Output:
output

Leave a Reply