The for loop is commonly used in JavaScript to execute a set of statements a specific number of times. Its syntax is as follows:
index.tsx78 chars4 lines
Here is an example of a for loop that prints the numbers 0 to 4 to the console:
index.tsx50 chars4 lines
This will output:
index.tsx10 chars6 lines
In the for loop declaration, i
is the variable being initialized and will be used to count the loop iterations. i < 5
is the condition that determines if the loop should continue executing. i++
is the statement that increments the value of i
on each iteration.
You can use any variable name in place of i
, as well as any condition that evaluates to a boolean.
gistlibby LogSnag