JavaScript syntax: Difference between revisions

Content deleted Content added
Nelsonkam (talk | contribs)
Do … while loop: Improved formatting for better rendering on mobile version.
Tags: Mobile edit Mobile web edit
Nelsonkam (talk | contribs)
For … in loop: Improved formatting for better rendering on mobile version.
Tags: Mobile edit Mobile web edit
Line 1,063:
 
===For … in loop===
The syntax of the JavaScript <code>[[Foreach|for … in loop]]</code> is as follows:
 
<syntaxhighlight lang=JavaScript>
Line 1,073:
* Iterates through all enumerable properties of an object.
* Iterates through all used indices of array including all user-defined properties of array object, if any. Thus it may be better to use a traditional for loop with a numeric index when iterating over arrays.
* There are differences between the various Web browsers with regard to which properties will be reflected with the for...in loop statement. In theory, this is controlled by an internal state property defined by the ECMAscript standard called "DontEnum", but in practice, each browser returns a slightly different set of properties during introspection. It is useful to test for a given property using <ttcode>if (some_object.hasOwnProperty(property_name)) { ... }</ttcode>. Thus, adding a method to the array prototype with <ttcode>Array.prototype.newMethod = function() {...}</ttcode> may cause <code>for … in</code> loops to loop over the method's name.
 
===While loop===