Loops and Functions in ES6 part 1

Loops and Functions in ES6
14
1/3













Loops in ECMAScript 6
In JavaScript we commonly use the for loop to iterate over values in a list:
The for...in loop is intended for iterating over the enumerable keys of an object.
For example:
The for...in loop should NOT be used to iterate over arrays because, depending on the JavaScript engine, it could iterate in an arbitrary order. Also, the iterating variable is a string, not a number, so if you try to do any math with the variable, you'll be performing string concatenation instead of addition.
ES6 introduces the new for...of loop, which creates a loop iterating over iterable objects.
For example:
During each iteration the val variable is assigned the corresponding element in the list.
The for...of loop works for other iterable objects as well, including strings:
The for...of loop also works on the newly introduced collections (Map, Set, WeakMap, and WeakSet). We will learn about them in the upcoming lessons.
Note that ES6 code will run only in browsers that support it. Older devices and browsers that do not support ES6 will return a syntax error.
Comments
Post a Comment