Menu
Coddy logo textTech

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' variable

ex. 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 icon

Challenge

Easy

Create 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+)