Javascript Lesson 4 Loops
Javascript Lesson 4 Loops
com
courseCS.WordPress.com
Lesson-4-Loops
Download Lesson
A loop in programming is a control structure that allows a set of instructions to be repeated multiple times
based on a specified condition. Loops are used to automate repetitive tasks and efficiently process a
sequence of data or perform a block of code multiple times. They are an essential part of almost all
programming languages.
Loop is mostly used with arrays, therefore, we explained loop on based on arrays in next sections.
Expression 1 is executed (one time) before the execution of the code block.
Expression 2 defines the condition for executing the code block.
Expression 3 is executed (every time) after the code block has been executed.
https://coursecs.wordpress.com/courses/javascript/lesson-4-loops/ 1/15
11/21/23, 9:25 AM Lesson-4-Loops – courseCS.WordPress.com
Example Code 1:
Example Code 2:
And you can omit expression 1 (like when your values are set before the loop starts) as you can see in below
code.
Example Code 3:
Example Code 4: If you don’t know about length of a array, then you can use either “for in” or “for of” loop.
There is another method by using “for” loop that is shown in following code. Output is same just like above.
https://coursecs.wordpress.com/courses/javascript/lesson-4-loops/ 2/15
11/21/23, 9:25 AM Lesson-4-Loops – courseCS.WordPress.com
Note: If you omit expression 2, you must provide a break inside the loop. Otherwise the loop will never end.
This will crash your browser. Read about breaks in a later chapter of this tutorial.
Example Code 5:
Example Code 6:
Note: In case of for...in, the “key” will contain indexes (named-index in case of object and numbered-
indexes in case of array)
Importance of loop “for in” over “for” is that you don’t need to know length of total properties/elements of
objects.
https://coursecs.wordpress.com/courses/javascript/lesson-4-loops/ 3/15
11/21/23, 9:25 AM Lesson-4-Loops – courseCS.WordPress.com
Example Code 7: In following code, an object “person” is declared by using const keyword and then all
properties of the objects are displayed by using the loop.
It is noted that let is used declare “x”. It is note necessary to use let. You can use either const or let
according to your requirement. The const is necessary to use at the time of declaration of the object.
Note: Only enumerable properties are included when using a for...in loop to iterate over an object.
While, non-enumerable properties would not be included. It would be discussed in detail in lesson of
object.
Example Code 8: Following code is just like above but in this case “for in” loop is used for array.
Note: Do not use for in over an Array if you want to access array’s element in an order. By using this loop,
array values may not be accessed in the order you expect. If element’s order is important, then it is better
to use a for loop or a for of loop.
Note: Importance of loop “for of” over “for” is that you have few conditions related to loop.
Note: Importance of loop “for of” over “for in” is that it is used for many iterable objects but “for in” is only
useful for objects and arrays.
Note: In case of for…of, the “key” will contain element value instead of index number. You can use for…in in
case of both array and object but for…of is only useful for array.
Example Code 9: In following code, an array “numbers” is declared by using const keyword and then all
properties of the objects are displayed by using the loop.
Note: If you use “for in” loop here, then result of both codes would be same.
https://coursecs.wordpress.com/courses/javascript/lesson-4-loops/ 4/15
11/21/23, 9:25 AM Lesson-4-Loops – courseCS.WordPress.com
Example Code 10: In following code, a string “courseName” is used with loop.
Note: If you use “for in” loop here, then result of both codes would not be same. Error will be generated but
“for in” loop will treat string/any-other-iterable-objects differently.
Example Code 11: In following code, body of the loop would be executed as long as a variable (i) is less than
10.
1 let i=0
2 while (i < 10) {
3 console.log("value of i: ", i)
4 i++;
5 }
Note: If you forget to increase the variable used in the loop-body, the loop will never end. This will crash
your browser.
do {
// code block to be executed
}
while (condition)
Example Code 12: The example below uses a do while loop. The loop will always be executed at least once,
even if the condition is false, because the code block is executed before the condition is tested.
This code is just line code 14, but we just use do while loop.
https://coursecs.wordpress.com/courses/javascript/lesson-4-loops/ 5/15
11/21/23, 9:25 AM Lesson-4-Loops – courseCS.WordPress.com
1 let i=0
2 do {
3 console.log("value of i: ", i)
4 i++;
5 }
6 while (i < 10)
Note: If you forget to increase the variable used in the loop-body, the loop will never end. This will crash
your browser.
Example Code 13: Following code will iterate each element by using do while loop without calculating
length of the array. Similar code can be write by using while loop. Such type of code is also discussed in
top of for loop.
Example Code 14: In this code, same memory is used for var type same-variable in case of inside and
outside of loop. It is noted that variable can be redeclared in side loop.
1 var i=5
2 for(var i=0; i<10; i++){
3 console.log(i) //it will display values from 0 to 9
4 }
5 console.log(i) //it will display value 10 for variable i
But if you used let type, then separate memory is allocated for variables outside and inside loop. See
following code.
1 let i=5
2 for(let i=0; i<10; i++){
3 console.log(i) //it will display values from 0 to 9
4 }
5 console.log(i) //it will display value 5 for variable i
https://coursecs.wordpress.com/courses/javascript/lesson-4-loops/ 6/15
11/21/23, 9:25 AM Lesson-4-Loops – courseCS.WordPress.com
Note: You cannot change type of declaration inside loop. See example code 8 and 9. Both codes will
generate errors for redeclaration.
1 let i=5
2 for(var i=0; i<10; i++){
3 console.log(i)
4 }
5 console.log(i)
1 var i=5
2 for(let i=0; i<10; i++){
3 console.log(i)
4 }
5 console.log(i)
Exercises
Solve following exercises to enhance your learnt concepts related to this lesson.
1. Write a for loop that counts from 1 to 10 and displays each number in the console.
2. Create an array of your favorite fruits. Use a for loop to iterate through the array and display each fruit
in the console.
3. Write a while loop that counts from 1 to 5 and displays each number in the console.
4. Create an array of numbers from 10 to 1. Use a for loop to iterate through the array in reverse order and
display each number.
5. Write a for loop that counts from 1 to 20. For each number, check if it’s even or odd and display a
message in the console.
https://coursecs.wordpress.com/courses/javascript/lesson-4-loops/ 7/15
11/21/23, 9:25 AM Lesson-4-Loops – courseCS.WordPress.com
1 let counter = 1;
2
3 while (counter <= 5) {
4 console.log(counter);
5 counter++;
6 }
https://coursecs.wordpress.com/courses/javascript/lesson-4-loops/ 8/15