For-loop is used when you want to execute a block of code repeatedly for a particular amount of time.
Let’s consider an example, suppose you want to print “hello world” 5 times, what we will do is write console.log(“hello world”) five times.
Instead what we can do is used a for loop for same the with less effort, here’s how :
Output :
Hello world
For…in loop
JavaScript has “for…In” loop.“for In” loop is used to loop over objects or enumerable properties of an object, still confused?
Let’s us see an example that will clear your doubt:
Output :
1 : apple
2 : banana
Here, “I” represents the keys of the object and fruits[i] represent respective key values.
For…of
This loop can be used over array, maps, sets, objects, strings and more, this loop was introduced in ECMA script 6 (ES6), it is like the simple version of the general for-loop, it iterates through all the iterables present in the iterable.
You will understand easily with a simple example below: