Loops Source Code

Lesson Details:
October 21, 2020


I: Introduction

Students are required to take a course on JavaScript, which is a very important part of website development. It is very rich in features, and students are expected to be familiar with it. The purpose of this article is to outline the various features of JavaScript code.

II: Body

A: Loops

The loop is one of the most commonly used structures in JavaScript. The loop feature provides the programmer with the ability to repeatedly execute some code. A loop executes a block of code repeatedly, until the condition inside the loop is false. The condition inside the loop is checked before each execution of the loop body. The loop can be executed indefinitely, or just once.

JavaScript has three kinds of loops: for, while, and do/while loops. A for loop is used when you want to execute a block of code several times. The structure of the for loop is as follows:

for (initialization; condition; iteration) { }

Here, initialization, condition, and iteration are all optional. Initialization is performed only once before the first iteration. The condition is checked before each iteration. If the condition evaluates to false, iteration is skipped and next iteration is performed. If iteration is not given, it defaults to 1 meaning that the loop will iterate once. This structure is known as an infinite loop. For example:

var i = 0; for (i = 0; i < 5; i++) console.log(i); //0, 1, 2, 3, 4

In this case, i will be initialized to 0 and after the condition becomes false at i = 5 , i will be incremented by 1 , hence subsequent iterations will be skipped. The output will be 0 , 1 , 2 , 3 , 4 .

As you can see from the above example, the variable i was declared outside the for loop but was accessible within the for loop. This is known as a variable hoisting. That happens because a for statement is completely different from a normal block statement. In fact, a for statement does not have a block structure at all. It simply executes a block of code multiple times and it starts from top to bottom. In other words, whatever comes after for is considered to be part of the initialization expression. In the above example, since there were no statements between for and { , they were collapsed into one expression that evaluated to true . This means that the initialization expression was evaluated when the parser saw it in the source code, and so i was already declared when it came time to execute the loop body.

Now let’s look at a slightly more involved example:

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

In this case, we defined i outside of the for loop along with two other statements that also had access to i . Now we get a different result because we assigned i 5 values inside the loop body rather than inside the initialization expression. Since we did not initialize it in the initialization expression, we did not overwrite its previous value and it retained its old value (which was undefined ). Therefore, when we accessed it outside of the for loop we got its old value (which was 5 ) instead of undefined . We can see this in action because we accessed i after executing the entire body of the for -loop and after i had been incremented by 1 . Therefore, we should have seen 6 , but we didn’t. This shows that we can use variables declared prior to a for statement in an initialization expression and vice versa. However, if we declare variables in both places then we should initialize them in both places or else we might get unexpected results. For example:

var i = 5; var j = 10; var k = 20; var l = 30; for (i=0; i<5; i++) { var j = 0; var k = 0; var l = 0; j++; k++; l++; console.log(j + ", " + k + ", " + l); } console.log(j + ", " + k + ", " + l); console.log(j + ", " + k); //5, 5 10, 20 30 console.log(k); //10 console.log(l); //30

In this case, j , k , l are all initialized in the initialization expression while i is initialized inside the for statement itself. Now when we access j , k , l outside of the for statement, we get their initial values which are undefined , undefined , undefined respectively. Accessing j , k , l inside the for statement worked because they are hoisted up to top of the initialization expression after being declared in the for statement itself. Let’s look at another example where things might become confusing when they are declared in both places:

var i = 5; var j = 0; var k = 0; var l = 0; var m = 0; var n = 0; for (i=0; i<5; i++) { j++; k++; l++; m++; n++; console.log(j + ", " + k + ", " + l + ", " + m + ", " + n); } console.log(j + ", " + k + ", " + l); console.log(j + ", " + k); console.log(j); //5, 5 10, 20 30 undefined console.log(k); //10 console.log(l); //30 console.log(m); //30 console.log(n); //30

Here again we did not initialize m and n in either place and so their values were undefined . Therefore when we accessed them outside of the for statement ( m and n ), we got their initial values (which were zero) instead of undefined . When accessing them inside of the for statement ( m and n ), on the other hand, they were initialized during each iteration of the loop body so their values were changed on each iteration of the loop body. Therefore when they were accessed outside of the for statement they retained their new values (which were 30 ). Note that while m and n were initialized on every iteration of the loop body, j , k , and l were not but they had their values reset before each iteration of the loop body so they retained their initial values (which were zero). We can see this by accessing them outside of the for statement after executing the entire body of the for -loop since they were declared before the for statement but were not initialized until after its completion. So what happened? Since m and n are declared inside of the initialization expression whereas j , k , and l are declared outside of it, JavaScript hoists m and n up to top of initialization expression after declaring them but hoists j , k , and l up to top of code before they are declared! So m and n are not hoisted but j , k , and l are hoisted! This means that if you declare m , j , k , l in both places you need to initialize them in both places or else you will get unexpected results! For example:

var m = 0; var j = 0; var k = 0; var l = 0; var n = 0; var o = 0; var p = 0; var q = 0; var r = 0; var s = 0; var t = 0; var u = 0; var v = 0; var w = 0; var x = 0; var y = 0; var z = 0; var a1 = 0; var a2 = 0; var a3 = 0; for (a1=0; a1<5; a1++) { v++; x++; y++; z++; console.log(v + ", " + x + ", " + y + ", " + z); } console.log(v + ", " + x); console.log(v); //5 11 22 33 44 55 66 77 88 99 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100

loader
Course content