in

JavaScript For in loop | Understanding the Use of The For in Loop

In this tutorial we will be looking at the JavaScript For in loop which entails the importance of the For in loop in JavaScript. In our previous tutorial we looked at the JavaScript For loop and we explained clearly the importance of the For loop and its usefulness. If you haven’t gone through the previous tutorial you can do so using the link below

Read previous tutorial on: JavaScript For loop | Understanding the Use of The For Loop

For in Loop

JavaScript Objects can be looped through using the For in loop. The major function of this loop is to loop through the properties of any given object. In our upcoming tutorials we will be explaining more about the JavaScript objects but for the main purpose of this tutorial please understand that objects in JavaScript is just a collection of different properties and a property is a defined association between a name and a value.

The syntax for FOR IN LOOP is shown below

for (name of variable in object) {
    block to execute
}

EXAMPLE

The below example shows the looping of properties found in a given browser navigator.

<!DOCTYPE html>
<html lang="en">
<head>
   
   <title>FOR IN LOOP ILLUSTRATION </title>
</head>
<body>

   <script type = "text/javascript">
      
         var webProperty;
         document.write("<b>List OF Browser Navigator Object Properties</b><br /> ");        
         for (webProperty in navigator) {
            document.write(webProperty);
            document.write("<br>");
         }
         document.write ("<b>End of looping!<b>");
      </script>      
</body>
</html>

RESULT

FOR IN LOOP ILLUSTRATION

CONCLUSION

The For in loop is very important and can be used to loop through the properties of any object. We hope with this tutorial you clearly understand how to use the For in loop when looping through object properties.

NEXT TUTORIAL: JAVASCRIPT LOOP CONTROL

What do you think?

javascript for loop

JavaScript For loop | Understanding the Use of The For Loop

javascript loop control

JavaScript Loop Control | Learn How To Control Loops in JavaScript