For loop in TypeScript
TypeScript is a superset of JavaScript. It is a strongly typed language that provides static typing, which helps in catching errors making it ideal for large team collaborations. Being a programming language, TypeScript provides loops to iterate through a collection of items or execute a block of code multiple times. In this Answer, we will explore the implementation of for-loop in TypeScript.
for loop
A for loop is used to iterate through a collection of items or execute a code block for a predefined number of times. The iterable sequence can be a list, string, or a range. The syntax of for loop is given below:
for (initialization ; condition ; increment/decrement){// code block}
In the above syntax, we can see that for loop takes:
intialization: Here, we initialize an integer loop variable.condition: Here, we define the condition until which the code block should execute.increment/decrement: Here, we increase or decrease the value of the initialized variable.
Example
Let's explore how we can utilize a for loop and execute a code block 10 times:
for(let i = 0 ; i <= 10 ; i++) {console.log("Iteration number: "+ String(i+1));}
The code explanation is given below:
Line 1: We initialize the loop variable
iwith0value. The loop will keep executing until the conditioni<=10is true. On each iteration,i++increases the value ofiby1.Line 2: We print the statement
Iteration number:with the current iteration number.
TypeScript provides two distinct types of for-loops that cater to different scenarios:
for-inloopfor-ofloop
for-in loop
The for-in loop is primarily used to iterate through properties of an object, but we can also use it to iterate through an array or string. Below we give examples of both of the use cases:
Iterating through an object
The example below shows how we can use the for-in loop to iterate through the object properties:
const person = { name: "Bob", age: 20, profession: "Lawyer" , gender: "male" };for (const key in person) {console.log(key +" : "+ person[key])}
The code explanation is given below:
Line 1: We create an object with information related to a person. The object stores data in a key-value pair format.
Line 3: We use the
for-inloop to iterate through thepersonobject. Thekeyis the loop variable that stores the key of the object in every iteration.Line 4: We print the key and value (
person[key]) of the object.
Iterating through an array
The example below shows how we can use the for-in loop to iterate through an array:
var animals : any = [ "dog" , "cat" , "goat" , "sheep" ]for( var idx in animals ) {console.log( animals[idx] )}
The code explanation is given below:
Line 1: We create an array of animal names.
Line 3: We use the
for-inloop to iterate through theanimalsobject.idxis the loop variable that stores the array index of the current iteration.Line 4: We print the value of the array at index
idx.
for-of loop
The for-of loop is used to directly access the items of an array or characters of a string. Below we can see examples of using for-of loop to iterate through an array and a string.
Iterating through an array
The example below shows how we can use the for-of loop to iterate through an array:
var birds:any = ["sparrow" , "eagle" , "dove" , "crow" , "raven"]for(var bird of birds) {console.log(bird)}
The code explanation is given below:
Line 1: We create an array of bird names.
Line 3: We use the
for-ofloop to iterate through thebirdsobject.birdis the loop variable that stores the array item of the current iteration.Line 4: We print the value stores in the variable
bird.
Iterating through a string
The example below shows how we can use the for-of loop to iterate through a string:
var word:string = "Hello"for(var char of word) {console.log(char)}
The code explanation is given below:
Line 1: We create a string with
Hellovalue.Line 3: We use the
for-ofloop to iterate through thewordstring.charis the loop variable that stores a character of the string in each iteration.Line 4: We print the value stores in the variable
char.
Conclusion
for loop is an important tool in any programmer's toolkit. It helps in executing a block of code multiple times according to a pre-defined condition.
Learn about do-while loops
Learn about while loops
Free Resources