;

Learn While Loop in Basic Javascript

Introduction to While in Basic Javascript



buayaberdiri.blogspot.com - In JavaScript, loops are used to repeat a block of code until a certain condition is met. One type of loop is the while loop, which is used when you need to repeat a block of code as long as a certain condition is true. In this article, we'll explore how to use while loops in JavaScript.

Basic syntax


The basic syntax of a while loop in JavaScript is as follows:


while (condition) {
  // Code to be executed while the condition is true
}

The condition is a boolean expression that is checked before each iteration of the loop. If the condition is true, the code inside the loop will be executed. This process will repeat until the condition is false.

Here's an example of a simple while loop that counts from 1 to 5:


let count = 1;

while (count <= 5) {
  console.log(count);
  count++;
}


In this example, we've initialized a variable called count to 1. The while loop checks whether count is less than or equal to 5 before each iteration. If the condition is true, the loop will execute the code inside, which logs the value of count to the console and increments it by 1. This process will repeat until count is greater than 5.

Infinite loops


One common mistake when using while loops is creating an infinite loop, where the condition is always true and the loop never exits. For example:


while (true) {
  // Code that never exits
}

This loop will continue to execute indefinitely, as the condition true is always true.

To avoid infinite loops, it's important to make sure that the condition will eventually become false, or to include an exit condition that will break the loop.

Exiting a loop


To exit a while loop before the condition is false, you can use the break statement. For example:


let count = 1;

while (true) {
  console.log(count);
  count++;

  if (count > 5) {
    break;
  }
}


In this example, we've used an infinite loop to count from 1 to 5. We've included an if statement inside the loop that checks whether count is greater than 5. If it is, the break statement is executed, which exits the loop.

Skipping an iteration


To skip an iteration of a while loop and move on to the next iteration, you can use the continue statement. For example:


let count = 1;

while (count <= 5) {
  if (count === 3) {
    count++;
    continue;
  }

  console.log(count);
  count++;
}

In this example, we've added an if statement inside the loop that checks whether count is equal to 3. If it is, the continue statement is executed, which skips the current iteration and moves on to the next one. This means that the value 3 will not be logged to the console.

Conclusion


while loops are a useful tool in JavaScript for repeating a block of code as long as a certain condition is true. By understanding the basic syntax and how to avoid infinite loops, exit the loop early, and skip iterations, you can use while loops to make your code more efficient and effective.


See the article about javascript here:

  1. Learn Array Method - forEach, map, reduce, filter
  2. Learn Basics Array In Javascript
  3. Learn Basics Asynchronous In Javascript
  4. How To Change String Value Using Replace in Javascript
  5. How to Learn a Calculator Basic in Javascript
  6. What Are Switch in Javascript And How to Use Them
  7. Create a Calculator Using the Python Flask Framework
  8. Basic Learning to Make a Calculator With PHP and HTML
  9. How to Use a FOR Loop in Javascript
  10. Introduction Objects Basics in Javascript
  11. Learn IF Else and Else IF in Basic Javascript
  12. Learning Functions in Basic Javascript



Check out other articles about HTML & CSS:

  1. Learn Input Types In HTML For Login And Register Form
  2. Learn HTML List - How To Create Square, Circle, Number List
  3. How To Create Comments Line In HTML
  4. How to Add Text Formatting In HTML
  5. Adding The Fieldset Code To The Form In HTML
  6. How to Create a Search Box in Pure HTML
  7. Create Color Charts With Pure HTML
  8. How to change font size using CSS
  9. Types of Border Styles in CSS Code
  10. How to Change the Background Color in CSS




List of Article Posts https://buayaberdiri.blogspot.com