Home >Web Front-end >JS Tutorial >Explain how to use the for...in statement in JavaScript_Basic knowledge
Here is another loop supported by JavaScript. It's called a for...in loop. This loop is used to loop through the properties of an object.
Using this loop may feel a bit unclear since we don’t have an object to discuss yet. However, once you understand JavaScript objects, you will find this loop very useful.
Grammar
for (variablename in object){ statement or block to execute }
Each iteration from the object assigns an attribute to the variable name (variablename), and this loop continues until all attributes of the object are exhausted.
Example:
The following is to print out the properties of the Navigator object of the web browser, as in the following example:
<script type="text/javascript"> <!-- var aProperty; document.write("Navigator Object Properties<br /> "); for (aProperty in navigator) { document.write(aProperty); document.write("<br />"); } document.write("Exiting from the loop!"); //--> </script>
This will produce the following results:
Navigator Object Properties appCodeName appName appMinorVersion cpuClass platform plugins opsProfile userProfile systemLanguage userLanguage appVersion userAgent onLine cookieEnabled mimeTypes Exiting from the loop!