top

Search

JavaScript Tutorial

The for…of loop was introduced in ES6 and is used to iterate over anything that has [Symbol.iterator] property. This includes array, strings and NodeList. They cannot be used to iterate over Objects are not iterable.If we check the __proto__ of an array, we can find the [Symbol.iterator] property.But it is not the case with Objects as it is not iterable.//Array example const array = ['a', 'b', 'c', 'd']; for (const item of array) { console.log(item) }// Output: a b c d//String example const string = 'Web Developer'; for (const character of string) { console.log(character) }//Output:W e b D e v e l o p e r //NodeList example const elements = document.querySelectorAll('.foo'); for (const element of elements) {    element.addEventListener('click', doSomething); }
logo

JavaScript Tutorial

for…of loop

The for…of loop was introduced in ES6 and is used to iterate over anything that has [Symbol.iterator] property. This includes array, strings and NodeList. They cannot be used to iterate over Objects are not iterable.

If we check the __proto__ of an array, we can find the [Symbol.iterator] property.

JavaScript Code

But it is not the case with Objects as it is not iterable.

JavaScript Code

//Array example
const array = ['a', 'b', 'c', 'd'];
for (const item of array) {
console.log(item)
}

// Output: 

a b c d
//String example
const string = 'Web Developer';
for (const character of string) {
console.log(character)
}

//Output:

W e b D e v e l o p e r

//NodeList example
const elements = document.querySelectorAll('.foo');
for (const element of elements) {
   element.addEventListener('click', doSomething);
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Comments

Janani

I have learned many things from this article. It is beneficial for me. Thank you!

Nilesh Chakrabarty

Nice example for beginners.. I m a beginner so this is very helpful for me ... so plz give this type of beginners example..

michael

This is a great introduction to variables in JavaScript! As a beginner to JavaScript, I found this guide very helpful in understanding the basics of variables and how they are used in JavaScript.

qtsinfo it

Thanks for sharing the information, it is very helpful, I hope to get more such beautiful blogs from you.

knowledge-wisdom

You have shared great information with me i appreciate your work!

Suggested Tutorials

Node JS Tutorial

Build various types of web applications,command-line application,etc.
Node JS Tutorial

Build various types of web applications,command-line application,etc....

Read More

Angular JS Tutorial

Introduction: Angular  (What is Angular?)Angular was formerly introduced by Google corporation in 2012 and was considered to be one of the most promising among JavaScript frameworks. It was written completely in JavaScript to separate an application’s logic from DOM manipulation, aiming at dynamic page updates. Angular introduced many powerful features enabling the developer to effortlessly create rich and single-page applications.Topics CoveredThis Angular tutorial will span over eight modules, each module covering numerous individual aspects that you need to gain complete information about Angular. This set of modules serves as an Angular tutorial for beginners along with experienced IT professionals.Here are the topics that will be covered in the Angular tutorial:Get started with Angular.Learn the basics of Angular.Know what Angular Directives.Get an idea of Component Inputs and Outputs of Angular.Know about Forms in Angular.About Services in Angular.Pipes in Angular.HTTP, Routing and Building in Angular.Who can benefit from this tutorial?This Angular tutorial will be helpful to IT professionals such as:Software Developers, Web Application Programmers and IT Professionals Software Architects and Testing Professionals Career aspirants in web development
Angular JS Tutorial

Introduction: Angular  (What is Angular?)Angular was formerly introdu...

Read More