Getting Around Flashcards
Common programming concepts
1
Q
When can a “For” loop be used?
Advantages?
A
Technique for Array iteration
+ uses iteration variable instead of accessing every property on every loop
+ Iteration var can be declared within the loop
2
Q
When is a ‘For … in’ loop used?
How does it work?
Important efficiency tip?
A
Technique for enumeration of properties in objects (non-arrays)
- accesses every property of the object
- Test with object.hasOwnProperty( ) to skip prototype properties
for (var i in myThing) { if(myThing.hasOwnProperty(i){ //loop stuff } }