for - of loop
Lesson 6 of 9 in Coddy's Tricky parts of Modern Javascript (ES6+) course.
The new for of loop is introduced in ES6.
It loops through the values of an iterable object.
Strings, Arrays and Maps are iterable objects in Javascript.
for (variable of iterable) {
// code block to be executed
}ex. Strings
let name = "Shubham";
for(let char of name){
console.log(char); // S,h,u,b,h,a,m
}
// in each iteration of the loop,
// the next character in string is assigned to 'char' variableex. Arrays
let list = ["Shubham",1,true];
for(let item of list){
console.log(item); // Shubham , 1 , true
}
// in each iteration of the loop,
// the next element in array is assigned to 'item' variable
Challenge
EasyCreate an array of characters from the string using for of loop and return the array.
Try it yourself
function getCharArray(str) {
// Write code here
}All lessons in Tricky parts of Modern Javascript (ES6+)
2Let's dive in
Block scope - let & constThe Arrow FunctionsSpread and Restfor - of loopClassesPromisesAsync & Await