👋 Hello! I'm Alphonsio the robot. Ask me a question, I'll try to answer.

How to write a for loops in JavaScript?

In JavaScript, the general syntax of a for loop is:

for ( [initialization] ; [condition] ; [increment]) {
	// code block to be executed
}

Here is an example of a simple loop displaying numbers from 1 to 10:

for (var i=1 ; i<=10 ; i++) {
	console.log (i);
}

Note that you can also use the following variants:

  • for ... in loops through the properties of an object ;
  • for ... of loops through the values of an iterable object.
More