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
- for(variable in object){
- ...........
- ...........
- ...........
- }
Here
- variable will store the different property names of the object coming from each iteration.
- object is that object whose enumerable properties are iterated.
Example
- <script>
- function getObjectValues(obj) {
- for (var item in obj) {
- alert(obj[item]);
- }
- }
- var obj = { prop1: 12, prop2: 100, prop3: 'Bikash' };
- getObjectValues(obj);
- </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 the
for..in loop we can get the value of every property of the object.