JavaScript Loops (Simplified)

JavaScript Loops (Simplified)

Simply put, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.

Loops

  • Loops allows us to automate repetitive tasks

  • Iterate is a generic term that means “to repeat” in the context of loops.

  • A loop will continue to iterate until a specified condition, commonly known as a stopping condition, is met.

Types of Loops


For Loops

  • A for loop declares looping instructions, with three important pieces of information separated by semicolons ;
  • The initialization defines where to begin the loop by declaring
  • The stopping condition determines when to stop looping
  • The iteration statement updates the iterator each time the loop is completed

  • Example

      for (int i = 0; i < 3; i++){
          console.log("your gay")
      }
    
      //output: your gay *4
    
  • Reverse for Loop

    • The loop will index from the last element in the loop to the first through subtraction
    • This would irritate the loop in reverse

      const items = ['apricot', 'banana', 'cherry'];
      
      for (let i = items.length - 1; i >= 0; i -= 1) {
      console.log(`${i}. ${items[i]}`);
      }
      
      // Prints: 2. cherry
      // Prints: 1. banana
      // Prints: 0. apricot
      
  • Nested for Loops

    • A nested for loop is when a for loop runs inside another for loop.
    • The inner loop will run all its iterations for each iteration of the outer loop.

    • Example

        for (let excercise = 1; excercise < 4; excercise ++) {
            console.log(`start excercise ${excercise}`)
            for (let rep = 1; rep < 3; rep ++) {
            conosle.log(`lifiting weight repetation ${rep}`)}
        }
      
        // output:
        start excercise 1
        lifiting weight repetation 1
        lifiting weight repetation 2
        lifiting weight repetation 3
        start excercise 2
        lifiting weight repetation 1
        lifiting weight repetation 2
        lifiting weight repetation 3
      

Looping Through Arrays

  • An array’s length can be evaluated with the .length property.
  • The .length of the array can be used as the stopping condition in the loop.

  • Reading the array

      for (let i = 0; i < jonas.length; i++) {
          console.log(jonas[i], typeof jonas[i]);
      }
    
  • Filling an array

      const types = [];
    
      for (let i = 0; i < jonas.length; i++) {
          types.push(typeof jonas[i]);
      }
    

While Loops

  • A while loop is a loop that keeps running as long as a specified condition is true
  • Use while loop If you want the loop to break based on a condition other than the number of times it runs.

  • Syntax

      while () {
      // code to be excuted
      }
    
    • between the () we write the condition
    • between the {} we write the actual code
  • Example

      while (i < 6) {
        console.log(i);
        i++;
      }
    
  • Infinite loop

    • by specifying a condition that is always true, we run an infinite loop

      while (true) {
        console.log("Hello, World!")
      }
      
      // this will print Hello, World! forever
      

Loop Control Statements


Continue

  • The continue keyword exist the irritation of the loop if a condition is not met

      for (let i = 0; i < bayak.length; i++) {
          if (typeof bayak[i]) !== "string") continue;
          console.log(jonas[i], typeof jonas[i]);
      }
    

Break

  • Unlike the continue keyword, the break keyword will stop the irritation of loop.

      for (let i = 0; i < bayak.length; i++) {
          if (typeof bayak[i]) !== "string") break;
          console.log(jonas[i], typeof jonas[i]);
      }