anyone can explain how to use for…in statement in javascript. I had read the w3school article but i think it is not so clear.Below is the code, please explain this:
<html>
<body>
<script type="text/javascript">
var x;
var mycars = new Array();
mycars[10] = "Saab";
mycars[20] = "Volvo";
mycars[30] = "BMW";
for (x in mycars)
{
document.write(mycars[x] + "<br />");
}
</script>
</body>
</html>
A
for inloop will iterate through every property in an object.In your example, the
xvariable will cycle through every property in themycarsobject.If you add
mycars.expensive = "Porsche";, it will find that too.Note that, as stated by MDC,
for inloops should not be used to loop through ordinary arrays: