HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / TypeScript / TypeScript ‘for…of’ Loop

TypeScript ‘for…of’ Loop

In TypeScript, You can iterate over iterable objects (including array, map, set, string, arguments object and so on) using for...of loop. To be an iterable, an object must implement the @@iterator method.

Loop over Array

Example of using 'for...of' to iterate over array elements.

let myArray = [10, 20, 30]; 
 
for (let value of myArray) { 
  console.log(value); 	//10 20 30
} 

Loop over Map

Example of using 'for...of' to iterate over map entries.

let map = new Map();
map.set("A",1);
map.set("B",2);
map.set("C",3);	

//Iterate over map keys
for (let key of map.keys()) {
    console.log(key);					//A B C
}

//Iterate over map values
for (let value of map.values()) {
    console.log(value);					//1 2 3
}

//Iterate over map entries
for (let entry of map.entries()) {
    console.log(entry[0], entry[1]);	//"A" 1 "B" 2 "C" 3
}

//Using object destructuring
for (let [key, value] of map) {
    console.log(key, value);			//"A" 1 "B" 2 "C" 3
} 

Loop over Set

Example of using 'for...of' to iterate over set entries.

let mySet = new Set();
mySet.add('A');
mySet.add('B');
mySet.add('C');

//Iterate over set 
for (let entry of mySet) {
    console.log(entry);		//A B C
}

Loop over String

Example of using 'for...of' to iterate over string. During iteration, you will get one single character from string in each loop cycle.

let blogName:string = "howtodoinjava.com";

//Iterate over set 
for (let character of blogName) {
    console.log(character);		//howtodoinjava.com
}

Drop me your questions in comments section.

Happy Learning !!

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. Jinisha

    April 9, 2020

    Hi Lokesh

    Great site. Thanks for making this. I have a suggestion. I see let being used in lot of places. Suggest you to use const instead.

Comments are closed on this article!

Search Tutorials

TypeScript Tutorial

  • TypeScript – Introduction
  • TypeScript – Types
  • TypeScript – Union Types
  • TypeScript – String Literal Types
  • TypeScript – var, let and const
  • TypeScript – Template Strings
  • TypeScript – Arithmetic Operators
  • TypeScript – Logical Operators
  • TypeScript – Comparison Operators
  • TypeScript – ‘for…of’ Loop
  • TypeScript – Spread Operator
  • TypeScript – Arrays
  • TypeScript – Enums
  • TypeScript – Map
  • TypeScript – Set
  • TypeScript – Functions
  • TypeScript – Function Overloading
  • TypeScript – Transpiler
  • TypeScript – Truthy and falsy
  • TypeScript – == vs ===
  • TypeScript – undefined vs null
  • TypeScript – Variable Hoisting
  • TypeScript – tsconfig.json

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces