Course Banner

Typescript Loops

Loops are essential part of any programing language. Using loop you can iterate same statement without writing that again and again.

Loop have 3  parts.

  1. Initialization
  2. Condition
  3. Increment or decrement

Example :

While Loop Example :

  1. i:number=0 // initilization
  2. while (i<=10){ // condition
  3.   console.log(i);
  4.   i++; // increment
  5. }

Output :

Do while Loop

    var i = 0;
  1. do{
  2. console.log(i);
  3. i++;
  4. }while (i <= 10);

For loop Example

    for(var i:number=1; i<=10;i++){
  1.  console.log(i);
  2. } loop Example :

Apart from for loop, TypeScript also support for in loop. The for… in loop can be used to repeat over a set of values as in the case of an array or a tuple.

The loop is used to iterate through a list or collection of values. But for for in loop the data type should be string or any.

Difference between for in and for of

‘For in’  displays the keys for the array while ‘for of ‘ displays the value list of the array.