Learning While loop, for loop in JavaScript and Break and Continue commands
6 minute(s) read | Published on: Jun 04, 2021 Updated on: Dec 14, 2021 |
JavaScript is one of the programming languages that has a lot of fans, and if it is used, it can cause users to get a better user experience when using the content which is created with the help of this language, so that, they will suggest your website to their friends, and as a result, more users will be attracted to your website.
There are various possibilities in this language that can be used to perform different actions, each of which has a wide world, and in order to be able to master them, it is necessary to pay attention to the different examples that exist for each of them.
This article gives a brief overview on While loop, for loop in JavaScript and Break and Continue commands, and we are going to give you examples, so that you can master this language, but before that we need to give a brief explanation about this language.
What is JavaScript?
JavaScript is one of the programming languages through which the content of a web can be converted from static to dynamic which enables the user to interact more with the web and programs created with the help of this language.
While-Loop:
It is one of the features in this language that if you use it, a block of code will be executed as long as the conditions mentioned are met.
However, if you are not careful while writing this code, the browser may crash because the loop never ends.
Note in the example below that the code in the loop will run until variable (i) is less than 10.
The result of the example will be as follows.
JavaScript While Loop
The number is 0
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
The number is 6
The number is 7
The number is 8
The number is 9
do / while:
This type of loop, executes a block of code once before it examines the existing conditions and then executes the block code until the condition is true.
The result of the example will be as follows.
Do/While-Loop
The number is 0
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
The number is 6
The number is 7
The number is 8
The number is 9
for-Loop:
These feature can be used in many ways, for example, if you want to code faster, and you want to execute a code over and over again with a different value, it is necessary to raise your information about this.
To be able to master this topic more, pay attention to the following example.
The result of the example will be as follows.
For Loop
BMW
Volvo
Saab
Ford
Fiat
Audi
In general, there are different types of loops, two of which we have mentioned above, the types of loops are as follows.
- The for / of Loop:
The JavaScript for / of statement loops through the values of an iterable object.
- The for / in Loop:
The JavaScript for / in statement loops through the properties of an Object.
Consider the following example.
The result of the example will be as follows.
For/In Loop
The for/in statement loops through the properties of an object. John Doe 25
- The For Loop:
It is used in order to repeat a code over and over again, with different values, in this article examples in this field are given.
- The while Loop:
If the conditions mentioned are met, a block of code will be executed.
- The do / while Loop:
This type of loop executes a block of code once before it examines the existing conditions and then executes the block of code until the condition is true.
Break and Continue:
Two of the things that exist in this language, and you can use it are called Break and Continue, which we will discuss in the following.
The Break Statement:
It is being used, if you want to jump out of a loop, you can see an example of this issue below.
The result of the above example will be as follows.
Loops
A loop with a break statement.
The number is 0
The number is 1
The number is 2
The Continue Statement:
In this case, if a specific condition is placed in the code, it will not execute that condition and will continue to execute the original condition again, which can be understood in the following example.
The result of the example will be as follows.
Loops
A loop with a continue statement.
A loop which will skip the step where i = 3.
The number is 0
The number is 1
The number is 2
The number is 4
The number is 5
The number is 6
The number is 7
The number is 8
The number is 9
Last word:
In general, mastering the JavaScript language is very important due to the fact that it is one of the most important programming languages which exist, and you need to be able to gain the necessary expertise in this language with effort and perseverance in order to achieve success, so in this article, we have mentioned the possibilities that exist in it, each of which can help different programmers to create pages with more productive content, and at the same time with proper coding they can ensure that everything they import will be executed correctly, but it should be noted that, the best way to learn the different things and the tips in it, is to pay close attention to the different examples that exist for each case and try them by yourself, you can also write code like that, so that you can gain the necessary skills in this popular language and create websites and programs that are of interest to the various users who use them.
Click to analyze your wesbite SEO