For Loop in JavaScript

Definition

The for.. in loop iterates over the enumerable properties of an object in a arbitrary order. For each different property the statements can be executed.

Syntax

  1. for(variable in object){  
  2. ...........  
  3. ...........  
  4. ...........   
  5. }   
Here

  • variable will store the different property name of the object coming from each iteration.

  • object is that object whose enumerable properties are iterated.

Example

  1. <script>  
  2.                        function getObjectValues(obj){  
  3.                                    for(var item in obj){  
  4.                                            alert(obj[item]);  
  5.                                    }  
  6.                        }  
  7.                        var obj = {prop1: 12,prop2: 100,prop3: 'Bikash'};  
  8.                        getObjectValues(obj);  
  9.            </script>  
It will give output as 12, 100, Bikash.

Uses

If we want to get the information of each and every properties of an object, then by applying for..in loop we can get the value of every properties of the object.

Ebook Download
View all
Learn
View all