Javascript for of loop

This loop came into the ES6. It is basically used to get the iteration value from the Array.
Syntax:-


for (variable of iterable) {
  // logic 
}

Example:-


var arr=[10,20,30];
for (var element of arr){
	
 console.log(element);
}
Output:- 10 20 30

Try it Yourself

Note:- you can use var, let and const to get the iteration value.

get the iteration value from the string


var data="John";
for( let element of data){

  console.log(element);

}
Output:- J
o
h
n

get the iteration value from the Array Object


var data=[{name:"John", age:35},{name:"Rom", age:30}]
for( var element of data){
	
	console.log("My name is "+element.name+" and age is "+element.age);

}
Output:-
My name is John and age is 35
My name is Rom and age is 30

Javascript for of loop – Interview Questions

Q 1: What is for...of loop?
Ans: Iterates over iterable values.
Q 2: Works on arrays?
Ans: Yes.
Q 3: Can it iterate strings?
Ans: Yes.
Q 4: Does it return index?
Ans: No.
Q 5: Introduced in which version?
Ans: ES6.

Javascript for of loop – Objective Questions (MCQs)

Q1. for...of is mainly used to iterate over ______.






Q2. Which of the following supports for...of?






Q3. or...of returns ______.






Q4. Which data structure can be iterated using for...of?






Q5. for...of was introduced in ______.