Loops in Javascript | what is loops? | Javascript 2020
Loops in Javascript
Loops In Javascript | Loop Statements is use to create a loop. Basically in Programming loop mean the repeatation of statements. So basically loop statements in Javascript is use to make the statement repeat.
What is the use of Loops?
Loops basically use to repeat a statement. For example, if we got task that print "hello world" 100 time, instant of typing "hello world" 100 we will simply use loop and we will write the statement only one type. There are many uses of loops like initialization of data for array etc.
While Loop
While loop is the type of loop which is use to repeat the statement on the basis of condition. While loop will stop only if its condition is false otherwise it will be an infinity loop. For example if we want to print 100 pages on printer only if the pages is save if not it will be terminated.
Syntax
while(condition) {
Block of Statements
}
In the above syntax if the condition is true the block of statement will be executed if not true the the loop will be terminated.
Example
var a =1;
while(a<10){
console.log("Hello World");
}
Do While Loop
Do While is similar to while loop but there ul is a few thing that it make it different from while loop. Do While loop is the type of loop which is use to repeat the statement on the basis of condition. In while loop condition is start at the beginning but in do while loop the condition is given at the bottom.Do While loop will stop only if its condition is false otherwise it will be an infinity loop. Do while loop will must execute for one time even if its condition is false.
Syntax
do{
Block of Statements
}while(condition);
In the above syntax if the condition is true the block of statement will be executed if not true the the loop will be terminated.
Example
var a =1;
do{
console.log("Hello World");
}while(a<10);
For loop
For Loops In Javascript | For loops is use to repeat the statement according to user needs. In this loop the user need to give the initial and final value to the loop. Javascript For Loop execpt three parameters.
Syntax
for(initialization; condition; increament/decreament) {
Block of statement
}
In for loop initialization means the initial value for the loop like from where we went to start our loop. The second is the the condition which is use for the finalization of loop like where we want to stop our stop our loop. The third part is the increment and decreament which is use to increase and decrease the value.
The block of statement will only executed if the condition is true otherwise it will terminated.
Example
for(var i = 0; i<10; >i++) {
console.log("Hello world");
}
This will print "Hello World" 10 time and when i become 10 so the loop will be terminated.
For in loop
For in loop is iterate through the properties of the object. It will return methods and properties of an object. At can also be use with an array.
Syntax
for( variable in <object>) {
Block of statement
}
In syntax the variable will be variable name and <object> will be the object. So it will return all possible properties.
Examples
for(var i in obj) {
console.log(obl[i].name);
}
It will run till the length of the properties in the object.
If you like our article you may also wanted to know about Get Time in javascript
Comments
Post a Comment