DEV Community

skptricks
skptricks

Posted on

Understanding Difference Between For, For…In and ForEach Loop In Javascript

Post Link : Understanding Difference Between For, For…In and ForEach Loop In Javascript

This tutorial explains basic implementation of For, For…In and ForEach loop in Javascript and it's working in real time scenarios. Loops in JavaScript are used to execute the same block of code a specified number of times or while a specified condition is true. Lets see the below example that helps you to build more understanding on For, For…In and ForEach loops.

Understanding Difference Between For, For…In and ForEach Loop In Javascript

For Loop
The for loop is used when you know in advance how many times the script should run. lets see the below example for For loop syntax, it will iterate for nth times.( depending upon looping times specify the nth value.)

Syntax :
for (i = 0; i < n; i++) {
// do something
}

Example
const array = ['skptricks', 'php', 'java'];
for (i = 0; i < array.length; i++) {
console.log(array[i])
}

Output:

"skptricks"
"php"
"java"

Understanding Difference Between For, For…In and ForEach Loop In Javascript

Read More...

Top comments (1)

Collapse
 
mogery profile image
Gergő Móricz

You should use markdown code blocks and put your code in them to make it look better!