JS Tutorial
JS Version
JS Objects
JS Function
JS Classes
JS Async
JS HTML DOM
JS Browser BOM
JS Web API
JS AJAX
JS JSON
JS vs JQUERY
JS Graphics
JavaScript For Of
The For Of Loop
The JavaScript for of statement loops through the values of an iterable object
It loops over iterable data structures like Arrays, Strings, Maps, NodeLists, and more:
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 defined with const, let, or var.
iterable – An object that has iterable properties.
Looping over an Array
Example
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript For Of Loop</h2>
<p>The for of statement loops through the values of any iterable object:</p>
<p id=”demo”></p>
<script>
const cars = [“BMW”, “Volvo”, “Mini”];
let text = “”;
for (let x of cars) {
text += x + “<br>”;
}
document.getElementById(“demo”).innerHTML = text;
</script>
</body>
</html>
Output
JavaScript For Of Loop
The for of statement loops through the values of any iterable object:
BMWVolvo
Mini
Looping over a String
Example
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript For Of Loop</h2>
<p>The for of statement loops through the values of an iterable object.</p>
<p id=”demo”></p>
<script>
let language = “JavaScript”;
let text = “”;
for (let x of language) {
text += x + “<br>”;
}
document.getElementById(“demo”).innerHTML = text;
</script>
</body>
</html>
Output
JavaScript For Of Loop
The for of statement loops through the values of an iterable object.
Ja
v
a
s
c
r
i
p
t