JavaScript for…in loop

The for…in loop is used to loop through an object’s properties. As we have not discussed Objects yet, you may not feel comfortable with this loop. But once you understand how objects behave in JavaScript, you will find this loop very useful.

Syntax

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

In each iteration, one property from object is assigned to variable name and this loop continues till all the properties of the object are exhausted.

Example

Try the following example to implement ‘for-in’ loop. It prints the web browser’s Navigator object.

<html>
   <body>
      
      <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>
      
      <p>Set the variable to different object and then try...</p>
   </body>
</html>

Output

Navigator Object Properties 
serviceWorker 
webkitPersistentStorage 
webkitTemporaryStorage 
geolocation 
doNotTrack 
onLine 
languages 
language 
userAgent 
product 
platform 
appVersion 
appName 
appCodeName 
hardwareConcurrency 
maxTouchPoints 
vendorSub 
vendor 
productSub 
cookieEnabled 
mimeTypes 
plugins 
javaEnabled 
getStorageUpdates 
getGamepads 
webkitGetUserMedia 
vibrate 
getBattery 
sendBeacon 
registerProtocolHandler 
unregisterProtocolHandler 
Exiting from the loop!
Set the variable to different object and then try...

Related Posts

  • 60
    JavaScript provides full control to handle loops and switch statements. There may be a situation when you need to come out of a loop without reaching at its bottom. There may also be a situation when you want to skip a part of your code block and start the next…
    Tags: loop, br, example, javascript
  • 48
    The 'for' loop is the most compact form of looping. It includes the following three important parts − The loop initialization where we initialize our counter to a starting value. The initialization statement is executed before the loop begins. The test statement which will test if a given condition is…
    Tags: loop, javascript
  • 47
    JavaScript Variable Scope The scope of a variable is the region of your program in which it is defined. JavaScript variables have only two scopes. Global Variables− A global variable has global scope which means it can be defined anywhere in your JavaScript code. Local Variables− A local variable will…
    Tags: variable, javascript
  • 43
    While writing a program, you may encounter a situation where you need to perform an action over and over again. In such situations, you would need to write loop statements to reduce the number of lines. JavaScript supports all the necessary loops to ease down the pressure of programming. The…
    Tags: loop, javascript
  • 38
    JavaScript Datatype One of the most fundamental characteristics of a programming language is the set of data types it supports. These are the type of values that can be represented and manipulated in a programming language. JavaScript allows you to work with three primitive data types − Numbers, 123, 120.50…
    Tags: javascript, variable

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: