2018-09-30 23:01:58 +01:00
---
id: cf1111c1c11feddfaeb5bdef
title: Iterate with JavaScript For Loops
challengeType: 1
2019-02-14 12:24:02 -05:00
videoUrl: 'https://scrimba.com/c/c9yNVCe'
2019-07-31 11:32:23 -07:00
forumTopicId: 18219
2021-01-13 03:31:00 +01:00
dashedName: iterate-with-javascript-for-loops
2018-09-30 23:01:58 +01:00
---
2020-11-27 19:02:05 +01:00
# --description--
2018-09-30 23:01:58 +01:00
You can run the same code multiple times by using a loop.
2020-11-27 19:02:05 +01:00
2021-03-02 16:12:12 -08:00
The most common type of JavaScript loop is called a `for` loop because it runs for a specific number of times.
2020-11-27 19:02:05 +01:00
2018-09-30 23:01:58 +01:00
For loops are declared with three optional expressions separated by semicolons:
2020-11-27 19:02:05 +01:00
2021-10-26 01:55:58 +09:00
`for (a; b; c)` , where `a` is the initialization statement, `b` is the condition statement, and `c` is the final expression.
2020-11-27 19:02:05 +01:00
2021-03-02 16:12:12 -08:00
The initialization statement is executed one time only before the loop starts. It is typically used to define and setup your loop variable.
2020-11-27 19:02:05 +01:00
2021-03-02 16:12:12 -08:00
The condition statement is evaluated at the beginning of every loop iteration and will continue as long as it evaluates to `true` . When the condition is `false` at the start of the iteration, the loop will stop executing. This means if the condition starts as false, your loop will never execute.
2020-11-27 19:02:05 +01:00
2021-03-02 16:12:12 -08:00
The final expression is executed at the end of each loop iteration, prior to the next condition check and is usually used to increment or decrement your loop counter.
2020-11-27 19:02:05 +01:00
2021-03-02 16:12:12 -08:00
In the following example we initialize with `i = 0` and iterate while our condition `i < 5` is true. We'll increment `i` by `1` in each loop iteration with `i++` as our final expression.
2019-05-17 06:20:30 -07:00
```js
2021-10-26 01:55:58 +09:00
const ourArray = [];
for (let i = 0; i < 5 ; i + + ) {
2019-05-17 06:20:30 -07:00
ourArray.push(i);
}
```
2021-10-26 01:55:58 +09:00
`ourArray` will now have the value `[0, 1, 2, 3, 4]` .
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --instructions--
2018-09-30 23:01:58 +01:00
2021-03-02 16:12:12 -08:00
Use a `for` loop to push the values 1 through 5 onto `myArray` .
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --hints--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
You should be using a `for` loop for this.
2018-09-30 23:01:58 +01:00
```js
2020-11-27 19:02:05 +01:00
assert(/for\s*\([^)]+?\)/.test(code));
```
2018-09-30 23:01:58 +01:00
2021-10-26 01:55:58 +09:00
`myArray` should equal `[1, 2, 3, 4, 5]` .
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert.deepEqual(myArray, [1, 2, 3, 4, 5]);
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
# --seed--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
## --after-user-code--
2018-09-30 23:01:58 +01:00
```js
2018-10-20 21:02:47 +03:00
if (typeof myArray !== "undefined"){(function(){return myArray;})();}
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
## --seed-contents--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
// Setup
2021-10-26 01:55:58 +09:00
const myArray = [];
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
// Only change code below this line
2021-10-26 01:55:58 +09:00
2020-11-27 19:02:05 +01:00
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --solutions--
2018-09-30 23:01:58 +01:00
```js
2021-10-26 01:55:58 +09:00
const myArray = [];
for (let i = 1; i < 6 ; i + + ) {
2018-09-30 23:01:58 +01:00
myArray.push(i);
}
```