208k views
4 votes
Which of the following is the proper way to create a 'for' loop?

loop (for i= minVal; i < maxVal; i++) {...}
for (var i= minVal; i++; i < maxVal) {...}
for (var i= minVal; i < maxVal; i++) {...}
for (i < maxVal) {...}

User Ruthless
by
7.8k points

1 Answer

2 votes

Final answer:

The proper way to create a 'for' loop in JavaScript is: for (var i = minVal; i < maxVal; i++).

Step-by-step explanation:

Loops are used in JavaScript to perform repeated tasks based on a condition. Conditions typically return true or false.

A loop will continue running until the defined condition returns false. The proper way to create a 'for' loop in JavaScript is:

for (var i = minVal; i < maxVal; i++) {...}

In this syntax:

  • var i = minVal; - initializes the loop variable 'i' with the initial value 'minVal'.
  • i < maxVal; - is the condition that must be true for the loop to continue.
  • i++ - updates the loop variable 'i' after each iteration.

A for loop repeats until a specified condition evaluates to false. The JavaScript for loop is similar to the Java and C for loop.

A for statement looks as follows: js Copy to Clipboard for (initialization; condition; afterthought)

User Augusto Destrero
by
8.7k points