Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is for....in statement in javascript

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>
like image 951
dramasea Avatar asked Apr 29 '26 20:04

dramasea


2 Answers

A for in loop will iterate through every property in an object.

In your example, the x variable will cycle through every property in the mycars object.

If you add mycars.expensive = "Porsche";, it will find that too.


Note that, as stated by MDC, for in loops should not be used to loop through ordinary arrays:

Although it may be tempting to use this as a way to iterate over an Array, this is a bad idea. The for...in statement iterates over user-defined properties in addition to the array elements, so if you modify the array's non-integer or non-positive properties (e.g. by adding a "foo" property to it or even by adding a method or property to Array.prototype), the for...in statement will return the name of your user-defined properties in addition to the numeric indexes. Also, because order of iteration is arbitrary, iterating over an array may not visit elements in numeric order. Thus it is better to use a traditional for loop with a numeric index when iterating over arrays. Similar arguments might be used against even using for...in at all (at least without propertyIsEnumerable() or hasOwnProperty() checks), since it will also iterate over Object.prototype (which, though usually discouraged, can, as in the case of Array.prototype, be usefully extended by the user where are no namespacing concerns caused by inclusion of other libraries which might not perform the above checks on such iterations and where they are aware of the effect such extension will have on their own use of iterators such as for...in).

like image 61
SLaks Avatar answered May 02 '26 10:05

SLaks


First you create an object with 3 items (and not an Array)

  var mycars = new Object();
  mycars[10] = "Saab";
  mycars[20] = "Volvo";
  mycars[30] = "BMW";

where 10, 20 and 30 are the object properties.
then you want to navigate through the object, visit all properties and display each value associated to a property.

This is where the [ for (variable in object) expression ] javascript construction intervenes:
The variable will be set to the first property of the object, then to the 2nd, then to the last. Try

  for (v in mycars) alert(v);

to see how it works, and this as well

  for (v in mycars) alert("Property: "+v+", value: "+mycars[v]);
like image 42
Déjà vu Avatar answered May 02 '26 09:05

Déjà vu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!