2018-10-10 18:03:03 -04:00
---
id: cf1111c1c11feddfaeb5bdef
2021-02-06 04:42:36 +00:00
title: Iterate with JavaScript For Loops
2018-10-10 18:03:03 -04:00
challengeType: 1
2020-04-29 18:29:13 +08:00
videoUrl: 'https://scrimba.com/c/c9yNVCe'
forumTopicId: 18219
2021-01-13 03:31:00 +01:00
dashedName: iterate-with-javascript-for-loops
2018-10-10 18:03:03 -04:00
---
2020-12-16 00:37:30 -07:00
# --description--
2020-04-29 18:29:13 +08:00
2021-02-06 04:42:36 +00:00
You can run the same code multiple times by using a loop.
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
The most common type of JavaScript loop is called a `for` loop because it runs "for" a specific number of times.
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
For loops are declared with three optional expressions separated by semicolons:
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
`for ([initialization]; [condition]; [final-expression])`
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
The `initialization` statement is executed one time only before the loop starts. It is typically used to define and setup your loop variable.
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
The `condition` statement is evaluated at the beginning of every loop iteration and will continue as long as it evaluates to `true` . When `condition` is `false` at the start of the iteration, the loop will stop executing. This means if `condition` starts as `false` , your loop will never execute.
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00: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.
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00: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` .
2018-10-10 18:03:03 -04:00
```js
var ourArray = [];
for (var i = 0; i < 5 ; i + + ) {
ourArray.push(i);
}
2020-12-16 00:37:30 -07:00
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
`ourArray` will now contain `[0,1,2,3,4]` .
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
# --instructions--
2020-04-29 18:29:13 +08:00
2021-02-06 04:42:36 +00:00
Use a `for` loop to work to push the values 1 through 5 onto `myArray` .
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
# --hints--
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
You should be using a `for` loop for this.
2018-10-10 18:03:03 -04:00
```js
2021-02-06 04:42:36 +00:00
assert(/for\s*\([^)]+?\)/.test(code));
2018-10-10 18:03:03 -04:00
```
2021-02-06 04:42:36 +00:00
`myArray` should equal `[1,2,3,4,5]` .
2020-04-29 18:29:13 +08:00
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert.deepEqual(myArray, [1, 2, 3, 4, 5]);
2018-10-10 18:03:03 -04:00
```
2020-04-29 18:29:13 +08:00
2021-01-13 03:31:00 +01:00
# --seed--
## --after-user-code--
```js
if (typeof myArray !== "undefined"){(function(){return myArray;})();}
```
## --seed-contents--
```js
// Setup
var myArray = [];
// Only change code below this line
```
2020-12-16 00:37:30 -07:00
# --solutions--
2021-01-13 03:31:00 +01:00
```js
var myArray = [];
for (var i = 1; i < 6 ; i + + ) {
myArray.push(i);
}
```