Javatpoint Logo
Javatpoint Logo

TypeScript Definite Loop

In definite loops, the number of iterations is known before beginning to execute the block of statements. The for loop is an example of a definite loop.

TypeScript for loop

The for loop is used to execute the block of code to a given number of times, which is specified by a condition. The syntax of the for loop is given below.

Syntax

In the for loop, the first expression is executed before the beginning of the loop, the second expression is the condition for the loop to execute, and the third expression is executed after the execution of every code block.

TypeScript Definite Loop

Example

Output:

The factorial of the given number is: 24

TypeScript for..of loop

The for..of loop is used to iterate and access the elements of an array, string, set, map, list, or touple collection. The syntax of the for..of loop is given below.

Syntax

Example

Output:

1
2
3
4
5

TypeScript for..in loop

The for..in loop is used with an array, list, or tuple. This loop iterates through a list or collection and returns an index on each iteration. The syntax of the for..in loop is given below.

Syntax

Example

Output:

Index of J: 0
Index of a: 1
Index of v: 2
Index of a: 3
Index of T: 4
Index of p: 5
Index of o: 6
Index of i: 7
Index of n: 8
Index of t: 9

for..of Vs for..in Loop

Both the loops iterate over the lists, but their kind of iteration is different. The for..in loop returns a list of indexes on the object being iterated, whereas the for..of loop returns a list of values of the object being iterated.

Below example demonstrates these differences:

Output:

Output of for..in loop 
0
1
2
3
4
Output of for..of loop 
10
20
30
40
50




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


B.Tech / MCA