For Loops
- For loop: for(initiation(optional),condition,increment(optional))
- var i = 2;
len = cars.length;
var text = "";
for (; i < len; i++) {
text += cars[i] + "<br>";
} - If you omit statement 2, you must provide a break inside the loop. Otherwise the loop will never end. This will crash your browser.
- For in: for(variableName in objectname)
- var person = {fname:"John", lname:"Doe", age:25};
var text = "";
var x;
for (x in person) {
text += person[x];
}
It is a common mistake, among new JavaScript developers, to believe that this code returns undefined:
It is true in many programming languages, but not true in JavaScript.
Example:
for (var i = 0; i < 10; i++) {// some code}
return i;
return i;
The code returns value of i as "10", though ideally it is out of the for loop. Thats how it is in javascript
_____________________________________________________________
Great info, Diana!
ReplyDelete