Thursday, 21 January 2016

JavaScript For Loop

<!DOCTYPE html>
<html>
<body>
<script>
cars=["BMW","Volvo","Saab","Ford"];
for (var i=0;i<cars.length;i++)
{
document.write(cars[i] + "<br>");
}
</script>
</body>

</html>
.......................................
The For/In Loop
<!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>
</html>

0 comments:

Post a Comment