English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
JavaScript Statements and Variable Declarations
for...ofThe statement creates a loop to traverse an iterable object, including: built-in String, Array, array-like objects (such as arguments or NodeList), and user-defined iterable objects.
It calls a custom iteration hook that contains statements to be executed for each different property value of the object.
Both for...in and for...of statements iterate over certain content. The main difference between them lies in the content they iterate over:
for...in - Traversal of the enumerable properties of an object in any order
for...of - Traversal of iterable objects defines the data to be traversed, creates an iteration loop on iterable objects (including Array, Map, Set, String, TypedArray, arguments object, etc.), calls a custom iteration hook, and executes statements for each different property value.
for (variable of iterable) { //Statement to be executed }
let iterable = [10, 20, 30, 40, 50]; for (let x of iterable) { document.write(x); }Test and See‹/›
The numbers in the table specify the first browser version that fully supports the for ... of statement:
Statement | |||||
for...of | 38 | 13 | 25 | 8 | Not Supported |
Parameter | Description |
---|---|
variable | Assign the values of different properties tovariable |
iterable | Iterate over objects whose properties are iterable |
JavaScript Version: | ECMAScript 1 |
---|
JavaScript Reference:JavaScript for...in Statement