Learning JavaScript: for Loops

Learning JavaScript: for Loops

In this article I’m going to cover the different forms of the for loop that are available in JavaScript. There is the general for loop, the for..in loop, and the for..of loop. I will describe how each loop works and when is the right time to use each loop type. I am leaving out the Array.forEach loop as it is specialized for arrays and requires some knowledge of functions I haven’t covered yet.

In this article I’m going to cover the different forms of the for loop that are available in JavaScript. There is the general for loop, the for..in loop, and the for..of loop. I will describe how each loop works and when is the right time to use each loop type.

I am leaving out the Array.forEach loop as it is specialized for arrays and requires some knowledge of functions I haven’t covered yet.

The General for Loop

The first form of for loop I want to discuss is the general for loop that is standard in most programming languages. This loop is used in situations where you know in advance how many times you want the loop to iterate, as opposed to while loops, which should be used primarily when the number of iterations is unknown when the program is written, such as when you’re processing an unknown quantity of data or records in a file.

The syntax template for the general for loop is:

for (loop-variable-init; condition; loop-variable-modification) {

statement(s);

}

The loop variable is a variable that is initialized, tested, and then modified until it causes the condition to become false. Here is a simple example of a for loop that prints the numbers 1 through 10:

for (let i = 1; i <= 10; i++) {
  putstr(i + " ");
}

The output from this program is:

1 2 3 4 5 6 7 8 9 10

Let’s see how this loop works with an array:

let names = ["Terri", "Meredith", "Allison", "Mason"];
for (let i = 0; i < names.length; i++) {
  putstr(names[i] + " ");
}

This program outputs:

Terri Meredith Allison Mason

You can have more than one statement in the loop body. The following program accepts input from the user and displays the sum of the 5 numbers entered:

let total = 0;
let number = 0;
const numEntries = 5;
for (let i = 1; i <= numEntries; i++) {
  putstr("Enter a number: ");
  number = parseInt(readline());
  total += number;
}
print("The total is: " + total);

Here is the output from one run of this program:

Enter a number: 1
Enter a number: 2
Enter a number: 3
Enter a number: 4
Enter a number: 5
The total is: 15

javascript learning-javascript learn-to-code learn-to-program programming-languages

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

13 Free/Low-Cost Sites to Supercharge Your Programming Self-Education

Although we still talk about programming as a standalone career, the dominance of technology in our lives makes it clear that coding is much more than a career path. In my opinion, computer science is more than a college major or a high-paid job; it’s a skill, essential for thriving in a modern-day economy. Whether you work in healthcare, marketing, business, or other fields, you will see more coding and have to deal with a growing number of technologies throughout your entire life.

Learning to Code: How to Boost Up the Process?

Learning to Code: How to Boost Up the Process? I also often recommend different online and offline resources to my students to make their learning process easier, more effective, and faster. And in this post, I will share a few tips with you.

Learning JavaScript: Computing with Object Methods

JavaScript has a set of built-in methods you can use with your user-defined objects. In this article I’m going to discuss several of these methods and how you can use them in your JavaScript programs.

Learning JavaScript: Working with Strings

Strings are the second most common data type used in JavaScript, and in many cases, since JavaScript is so widely used for web applications, it is the prominent data type.

Learning JavaScript: Data Types and Variables

To paraphrase the title of an old computer science textbook, “Algorithms + Data = Programs.” The first step in learning a programming language such as JavaScript is to learn what types of data the language can work with. The second step is to learn how to store that data in variables. In this article I’ll discuss the different types of data you can work with in a JavaScript program and how to create and use variables to store and manipulate that data.