2 Things before javascript loops practice

Posted under javascript sample code On By mohammed.mutawe

JavaScript loops practice

You must have studied the work of Loops in programming languages ​​or in the science of algorithms and you have a clear idea about it. Have you ever done JavaScript loops practice? If the answer is not yes, I will explain it right away. If you do not know what loops are in programming languages, you can read this article and then return to this page.


Why Loops?

The need for recursive sentences came when programmers wanted to escape from hard code, which increases time and boredom during the creation and manufacture of programs, and because we are talking here about the JavaScript language, relying on iterative loops is very important while dealing with data.

Perhaps there is no clear definition of iterative loops and there is no scientific document on it in a broad way, but iterative loops express a routine process that is executed for a large set of data, which ends when a counter continues to increase or decrease.


Reasons for using loops

There are clear reasons for using iterative loops, which were useful to many developers or even programmers. In this topic, I will focus on two essential reasons only:

  • Writing code in a professional way.
  • Shorten the time and efficiency of the program.


Professional Way

Getting code that is easy to modify and read is very difficult, and writing the code in a professional way is the main factor for the success of any program, and the reason for this is to have a very popular format and organization for all programmers, which leads to the maintenance of the code permanently.

If we assume that some of us, while studying programming languages, do JavaScript loops practice until they can code well and then read code with a large content, it will be easier for them to analyze that code, even if it is large.

Do not forget that some iterative loops may reach approximately 500 lines. Yes, this is the truth when calling big data and applying some conditions to each other while running the code to increase the size of the code and reach large numbers of lines.

Therefore, Loops is a professional way to write code that enables everyone to understand a code and use it again in their programs with some modifications.


Shorten The Time

Do JavaScript loops practice before you start doing your projects. Imagine you have an HTML tag consisting of a list li and this list contains 200 elements and you may have filled them manually before but I will assume you fetched them from a database via Backend languages ​​and you want to pass over each element in JavaScript With all of these elements printed inside the HTML page.

Well, if you do not have a strong knowledge of programming languages, you will face many difficulties in fetching this data, which may have to be printed one by one, which hinders your performance in executing the program quickly or moving to the next stage of development.

So one of the main advantages of JavaScript loops practice is to build your complex code in a short time without the hassle of traditional programming methods and this is probably a feature of all programming languages ​​and not only with JavaScript.


Types Of Loops

Loops in programming languages ​​are standard and of a uniform style, with some Syntax different for each language, but they follow the pattern of ancient mother languages ​​such as C++, PHP, and others, which consist of:

  • For Loops(Most Popular).
  • While Loops.
  • Do While.


For Loops

The For structure is the closest to the structure of conditional sentences. It includes the word For and two brackets(), and between them is a counter that calculates a set of rounds and rounds, which are counted between the Two Brackets{}, so that the form of the sentence in JavaScript is as follows:

<script type="text/javascript">
for(var i = 0; i<=20 ; i++ )
  /*Your Code Here*/

I have defined an iterative loop in this code consisting of a variable i, which calculates the number of rounds in the iteration, which was 20 cycles, and this means that I told the CPU to define a counter of type integer and its value is zero and if the value is still less than or equal to 20 then increase this variable and after each In addition, enter Body and execute the code inside, then return again until you finish all the cycles, which will stop when you reach the number 20.

When you apply this code in your browser, nothing will happen. Why?
Because we have reserved the For clause in memory to run without executing any internal code. If you want to see the result yourself, copy the code into your browser and print any value between brackets to see how Looping is work.

While Loops

While loop method is easy to implement and you can do JavaScript loops practice by using While Loop, all you have to do is write the reserved word While in the editor, parentheses and a variable unless it meets the condition, open parentheses at the bottom, increase the value of the variable you defined previously , and then execute your code so that the appearance of the code is as follows:

<script type="text/javascript">
var x = 0;
  document.write("<h2>Hello World</h2>");

I do not think that you will find it difficult, as it is not very different from the previous For Loops and it works exactly as it should, but some programmers prefer this method instead of For or Do while.

Do While

I write Looping sentences without even referring to JavaScript sources because they are similar in programming languages. We now come to the last method, which is Do While, which is not much different from the previous While, but some prefer it in the code like this:

<script type="text/javascript">
c = 0;
  document.write("<h5>Hello Javascript!</h5>");
}while(c <=5)

Note with me that I defined a variable and gave it a zero value. Then I used a reserved statement do and parentheses and inserted the code inside with increasing the value of the variable in each cycle and put the condition at the end of the code.



  • In do and while loops define an counter outside to avoid stack.
  • Write the recursive sentences correctly because not setting a closed value will cause your computer processor to overheat, so do not use the Infinite loop too much.
  • The JavaScript loops practice is the only way to master recursive sentences in this language.
  • It’s recommended to review other languages loops.



  • It’s partially referenced with Third Edition JavaScript Book.

Leave a comment

Your email address will not be published. Required fields are marked *