...

Web Technologies - JAVASCRIPT

Back to Course

Lesson Description


Lession - #234 Javascript Loop For Of


JavaScript For Of

The For Of Loop

The for of statement in JavaScript runs through the values of an iterable object.
It allows you to loop over iterable data structures like Arrays, Strings, Maps, NodeLists, and others:
Syntax
for (iterable variable>
{
//to be run code block
}

variable - The value of the next property is changed with each iteration. given to the variable Variables can be declared using the syntax const, let, or var.
iterable - An object whose properties can be iterated.

JavaScript gained browser support for/of in 2015. (ES6>

Safari 7 was the first browser to include support for:

Sorry cannot be displayed Sorry cannot be displayed Sorry cannot be displayed Sorry cannot be displayed Sorry cannot be displayed
Chrome 38 Edge 12 Firefox 51 Safari 7 Opera 25
Oct 2014 Jul 2015 Oct 2016 Oct 2013 Oct 2014
For/of is not supported in Internet Explorer.
Internet Explorer does not support the for/of operator.

Looping through an Array

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript For Of Loop</h2>
<p>Any iterable object's values are looped around using the for of statement:</p>

<p id="demo"></p>

<script>
const names = ["King","Queen","Minister"];

let text = "";
for (let x of names>
{ text += x + "<br><br>"; } document.getElementById("demo">
.innerHTML = text; </script> </body> </html>

Output:
Sorry cannot be displayed
Try it here



Looping through a String

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript For Of Loop</h2>

<p>The for of statement loops through an iterable object's values.</p>

<p id="demo"></p>

<script>
let language = "Programming";

let text = "";
for (let x of language>
{ text += x + "<br>"; } document.getElementById("demo">
.innerHTML = text; </script> </body> </html>

Output:
Sorry cannot be displayed
Try it here