-
Notifications
You must be signed in to change notification settings - Fork 2
Description
The For/In Loop
The JavaScript for/in statement loops through the properties of an object:
Example
var person = {fname:"John", lname:"Doe", age:25};var text = "";
var x;
for (x in person) {
text += person[x];
}The For/Of Loop
The JavaScript for/of statement loops through the values of an iterable objects
for/of lets you loop over data structures that are iterable such as Arrays, Strings, Maps, NodeLists, and more.
The for/of loop has the following syntax:
for (variable of iterable) {
// code block to be executed}
variable - For every iteration the value of the next property is assigned to the variable. Variable can be declared with const, let, or var.
iterable - An object that has iterable properties.
Looping over an Array
Example
var cars = ['BMW', 'Volvo', 'Mini'];
var x;for (x of cars) {
document.write(x + "
");
}