;

How to Use a FOR Loop in Javascript

How to Use For in Javascript


How to write For Loop Statement in Javascript


buayaberdiri.blogspot.com - is a popular programming language used for building web applications, and one of the most commonly used control structures in JavaScript is the "for loop". A for loop is a programming construct that allows you to repeat a block of code multiple times based on a specified condition. In this article, we will explore the for loop in JavaScript in-depth.

Syntax of a for loop

The syntax of a for loop in JavaScript is as follows:


for ([initialization]; [condition]; [final-expression]) {
  // code block to be executed
}


The initialization statement is executed only once, at the beginning of the loop. The condition is evaluated at the beginning of each iteration, and if it is true, the code block is executed. After the code block is executed, the final-expression is executed, and then the condition is evaluated again. If the condition is still true, the code block is executed again, and the process continues until the condition is false.

Example of a for loop


Let's take a look at an example to understand the for loop in JavaScript better. Suppose you want to print the numbers from 1 to 10. You can use the following for loop to achieve this:


for (var i = 1; i <= 10; i++) {
  console.log(i);
}

In this example, the initialization statement initializes the variable i to 1. The condition is that i should be less than or equal to 10. The final-expression increments i by 1 after each iteration. Therefore, the code block is executed 10 times, and each time the value of i is printed to the console.

The initialization, condition, and final-expression statements in a for loop are optional. If you omit the initialization statement, the loop will use the existing value of the variable. If you omit the condition, the loop will continue infinitely unless you break out of it using a break statement. If you omit the final-expression, the loop will continue until the condition is false.

Using a for loop with arrays


You can use a for loop to iterate over the elements of an array. Here's an example:


var fruits = ["apple", "banana", "orange", "grape"];

for (var i = 0; i < fruits.length; i++) {
   console.log(fruits[i]);
}


In this example, the condition is that the variable i should be less than the length of the fruits array. The final-expression increments i by 1 after each iteration. Therefore, the code block is executed once for each element of the array, and each time the value of the current element is printed to the console.

Using a for loop with objects


You can also use a for loop to iterate over the properties of an object. However, unlike arrays, objects do not have a length property, so you need to use a different condition to determine when to stop iterating. Here's an example:


var person = {name: "John", age: 30, occupation: "developer"};

for (var key in person) {
   console.log(key + ": " + person[key]);
}


In this example, the for...in loop iterates over the properties of the person object. The key variable represents the current property name, and person[key] represents the current property value. Therefore, the code block is executed once for each property of the object, and each time the name and value of the current property are printed to the console.

Conclusion


In conclusion, the for loop is a powerful construct in JavaScript that allows you to execute a block of code multiple times based on a specified condition. You can use it to iterate over arrays and objects, or to perform any other operation that requires repetitive execution. 



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




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