Saturday 25 January 2014

for in loop

JavaScript for...in Loop
The JavaScript for...in statement loops through the properties of an object.
Syntax
for (variable in object)
  {
  code to be executed
  }
Note: The block of code inside of the for...in loop will be executed once for each property.
Example
Looping through the properties of an object:
Example
var person={fname:"John",lname:"Doe",age:25}; 

for (x in person)
  {
  txt=txt + person[x];
  }
<!DOCTYPE html>
<html>
<body>
<p>Click the button to loop through the properties of an object named "person".</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>

<script>
function myFunction()
{
var x;
var txt="";
var person={fname:"John",lname:"Doe",age:25};

for (x in person)
{
txt=txt + person[x];
}

document.getElementById("demo").innerHTML=txt;
}
</script>
</body>

<!-- http://improvejavascript.blogspot.in/-->

</html>

No comments:

Post a Comment