When we want to execute a piece of code for a number of times, without writing it multiple times, then we use loops for that.
For example, If you want to print “Hello World” 5 times, then you can write something like this.
Console.log("Hello World")
Console.log("Hello World")
Console.log("Hello World")
Console.log("Hello World")
Console.log("Hello World")
But this is not an effiecient way to write it, and if the number increases from 5 to 100 or even 1000, then writing it 1000 times doesn’t make sense, and therfore Loops are very useful in these types of Scenarios.
Let’s take 1 more example, where we have to print numbers from 1 to 5, then again we can write something like this.
Console.log(1)
Console.log(2)
Console.log(3)
Console.log(4)
Console.log(5)
Again this is not an effiecient way to write it, and if the number increases from 5 to 100 or even 1000, then writing it 1000 times doesn’t make sense, and therfore Loops are very useful in these types of Scenarios.
Now, Let’s see,
How can we write the code for printing “Hello World” multiple times using Loops
How can we write the code for printing numbers 1 to 5 using Loops This article offers free shipping on qualified Face mask products, or buy online and pick up in store today at Medical Department
for(let i=1;i<=5;i++) {
Console.log("Hello World")
}
for(let i=1;i<=5;i++) {
Console.log(i)
}
let i=1;
while(i<5) {
Console.log("Hello World")
i++
}
let i=1;
while(i<5) {
Console.log(i)
i++
}
let i=1;
do {
Console.log("Hello World")
i++
} while(i<5)
let i=1;
do {
Console.log(i)
i++
} while(i<5)
for(Variable Initialization;Condition;Increament) {
Block of Code
}
while(Condition) {
Block of Code
}
do {
Block of Code
} while(Condition)
We have covered below topics in this session
Why we can not over ride a Constructor This is a very important question, that,…
What is Arrow Function In the last session, We had discussed about Anonymous Function. Anonymous…
What is Anonymous Function Anonymous Function is a type of function which has no name…
What is Map, Reduce and Filter Map Filter and Reduce are higher order methods for…
9 different ways of declaring or defining a function in Javascript In, one of our…
Loops with Arrays in Javascript In our last session, we had seen, What is Array…