Learn JavaScript

JavaScript for of Loop

JavaScript for of Loop was introduced in the later version (ES6) of JavaScript. It is used to loop through the iterable objects like: Arrays, Sets, Strings, etc. In order to execute each iteration, the value of the next property is assigned to the variable.

Standard Syntax of JavaScript for of Loop

for (variable of iteration)
{
Statement 1;
Statement 2;
Statement 3;
.
.
.
Statement n;
}

Example: JavaScript for of Loop with String

<html>
<body>
<h2>Program to understand the working of for of loop</h2>
<script>
var user = 'STUDENT';
var x;
for(x of user)
{
document.write(x + "<br>");
}
</script>
</body>
</html>

Output

Program to understand the working of for of loop

S
T
U
D
E
N
T


Example: JavaScript for of Loop with Array

<html>
<body>
<h2>Program to understand the working of for of loop</h2>
<script>
var user = ['Johney', 'Sara', 'Kiara', 'Mitchel'];
var x;
for(x of user)
{
document.write(x + "<br>");
}
</script>
</body>
</html>

Output

Program to understand the working of for of loop

Johney
Sara
Kiara
Mitchel


Example: JavaScript for of Loop with Set

<html>
<body>
<h2>Program to understand the working of for of loop</h2>
<script>
var user = new Set (['Johney', 'Sara', 'Kiara', 'Mitchel']);
var x;
for(x of user)
{
document.write(x + "<br>");
}
</script>
</body>
</html>

Output

Program to understand the working of for of loop

Johney
Sara
Kiara
Mitchel


Audience

This JavaScript tutorial is designed to help the beginners as well as the professional developers. For better understanding the language of the tutorial is kept simple and easy to understand.

Prerequisites

This tutorial demands to have the basic knowledge of the programming languages such as c or c++, HTML, Sets, and objects for the better understanding of the JavaScript programming language. The other basic requirement to learn JavaScript through this tutorial is the basic knowledge of computer and information technology so you can easily acquire a better understanding JavaScript programming.

Community Helping

In case you find any kind of error like typing error, spelling mistakes, incorrect grammar, incorrect information or any other mistakes. Please feel free to write to us at: akyadav@etutororg.com.