JavaScript Loops and Jump Statements

Level : Beginner
Mentor: Shailendra Chauhan
Duration : 00:01:00

Loops:

Loops are used to execute a block of code repeatedly as long as a certain condition is met. They help automate repetitive tasks in programming.

For Loop:

The 'for' loop is used to iterate over a sequence of numbers or elements. It has three parts: initialization, condition, and iteration.

Example:

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

Nested For Loop:

A nested 'for' loop is when one 'for' loop is inside another. This is useful for handling multidimensional arrays or performing tasks with multiple levels of iteration.

Example:

for (let i = 0; i < 3; i++) {
  for (let j = 0; j < 2; j++) {
    console.log(i, j);
  }
} 

While Loop:

A 'while' loop repeatedly executes a block of code as long as a specified condition is true.

Example:

let count = 0;
while (count < 3) {
  console.log(count);
  count++;
} 

Do…While Statement:

The 'do...while' loop guarantees that the code block will execute at least once, even if the condition is initially false.

Example:

let num = 5;
do {
  console.log("Number:", num);
  num--;
} while (num > 0);

Looping Through Arrays:

Arrays can be looped through using their length and index to access each element.

Example:

const fruits = ['apple', 'banana', 'cherry'];
for (let i = 0; i < fruits.length; i++) {
  console.log(fruits[i]);
}

Reverse Loop:

A reverse loop iterates through an array in reverse order.

Example:

const numbers = [3, 6, 9];
for (let i = numbers.length - 1; i >= 0; i--) {
  console.log(numbers[i]);
}

Break Keyword:

The 'break' keyword is used to exit a loop prematurely when a certain condition is met.

Example:

for (let i = 0; i < 10; i++) {
  if (i === 5) {
    break;
  }
  console.log(i);
}

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