Year End Sale: Get Upto 40% OFF on Live Training! Offer Ending in
D
H
M
S
Get Now

Typescript Loops

Level : Intermediate
Mentor: Shailendra Chauhan
Duration : 00:01:15

While Loop:

The while loop is a control flow statement in TypeScript that repeatedly executes a block of code as long as a specified condition is true.

Example:

let count = 0;
while (count < 5) {
    console.log(`Count is ${count}`);
    count++;
}

Do...While Loop:

The do...while loop is similar to the while loop, but it ensures that the code block is executed at least once before checking the condition for repetition.

Example:

let num = 1;
do {
    console.log(`Number is ${num}`);
    num++;
} while (num <= 5);

For Loop:

The for loop is a compact control structure in TypeScript used for iterating over a range of values. It consists of an initialization, a condition, and an increment/decrement expression.

Example:

for (let i = 0; i < 5; i++) {
    console.log(`Iteration ${i}`);
}

For...In Statement:

The for...in statement in TypeScript is used to iterate over the enumerable properties of an object. It is typically used for iterating over object keys or array indices.

Example:

const person = { name: "John", age: 30, city: "New York" };
for (const key in person) {
    console.log(`${key}: ${person[key]}`);
}

For...Of Statement:

The for...of statement is used to iterate over the values of iterable objects such as arrays, strings, and collections. It simplifies the process of looping through elements.

Example:

const colors = ["red", "green", "blue"];
for (const color of colors) {
    console.log(`Color: ${color}`);
}
Self-paced Membership
  • 24+ Video Courses
  • 825+ Hands-On Labs
  • 400+ Quick Notes
  • 125+ Skill Tests
  • 10+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
CONTACT US
Accept cookies & close this