# Could somebody help explain this to me: NEW \[spoiler\]

**URL:** <https://forum.freecodecamp.org/t/could-somebody-help-explain-this-to-me-new-spoiler/385723>\
**Category:** I Built This\
**Created:** [May 8, 2020, 2:33pm UTC](https://forum.freecodecamp.org/t/could-somebody-help-explain-this-to-me-new-spoiler/385723 "2020-05-08T14:33:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![MissingFable](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/missingfable/32/183194_2.png) [@MissingFable](https://forum.freecodecamp.org/u/MissingFable)\
**Post date:** [May 8, 2020, 2:33pm UTC](https://forum.freecodecamp.org/t/could-somebody-help-explain-this-to-me-new-spoiler/385723/1 "2020-05-08T14:33:42Z")

</div>

var myArray=  
var i=0  
while(i\<5){  
myArray.push(i)  
i++  
}  
console.log(myArray)

I understand why everything in the function is happening, but why does my console read [0, 1, 2, 3, 4], as in the function it states to increment the variable while under 5. Why is there a 0 in that array?

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [May 8, 2020, 2:36pm UTC](https://forum.freecodecamp.org/t/could-somebody-help-explain-this-to-me-new-spoiler/385723/2 "2020-05-08T14:36:14Z")

</div>

Your code logs `[0, 1, 2, 3, 4]` because those are the values of `i` starting at `0` and continuing until `i` is no longer smaller than `5`. What part of this is different from what you expect?

---

<div class="post-metadata">

**Author:** ![MissingFable](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/missingfable/32/183194_2.png) [@MissingFable](https://forum.freecodecamp.org/u/MissingFable)\
**Post date:** [May 8, 2020, 2:38pm UTC](https://forum.freecodecamp.org/t/could-somebody-help-explain-this-to-me-new-spoiler/385723/3 "2020-05-08T14:38:28Z")

</div>

So the console logs [0] because it has to state the original value of i before it can increment the variable

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [May 8, 2020, 2:42pm UTC](https://forum.freecodecamp.org/t/could-somebody-help-explain-this-to-me-new-spoiler/385723/4 "2020-05-08T14:42:10Z")

</div>

```auto
// Create new array
var myArray = [];

// Initialize iterator to 0
var i = 0

// Loop i = 0 to i = 4
while (i < 5) {
  // Push i to end of array
  myArray.push(i)
  // Increase iterator
  i++
}

// Log contents of array
// Contents are [0, 1, 2, 3, 4, 5]
console.log(myArray)

```

---

<div class="post-metadata">

**Author:** ![Catalactics](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/catalactics/32/151364_2.png) [@Catalactics](https://forum.freecodecamp.org/u/Catalactics)\
**Post date:** [May 8, 2020, 2:49pm UTC](https://forum.freecodecamp.org/t/could-somebody-help-explain-this-to-me-new-spoiler/385723/5 "2020-05-08T14:49:56Z")

</div>

This declares a variable name myArray with a value of an empty array `[]`

> [@MissingFable](#):
>
> var myArray=`[]`

Declares the variable `i` is equals to `0`

> [@MissingFable](#):
>
> var i=0

- This is a while loop, and it will run again and again until the condition is right. In this case, the condition is if the variable `i` smaller the number `5`.
- Well `i` was declared with a value of `0`, that means the code inside it will run. Then `myArray.push(i)` fills the variable myArray that have an empty array with the variable `i` which is currently 0.
- Then after that `i++` is just a shortcut programmers made _because we are lazy_ that just means `i = i + 1`. This means the variable `i` is modified.
- So after that the code goes back to the while and check is `i` smaller than 5. In this case, `i` has been changed into 1, but its still smaller than `5`.
- That means the code inside will run again until `i` is bigger than 5

> [@MissingFable](#):
>
> while(i\<5){  
> myArray.push(i)  
> i++  
> }

Finally, this just logs it to your console the variable `myArray`

> [@MissingFable](#):
>
> console.log(myArray)

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 1:19pm UTC](https://forum.freecodecamp.org/t/could-somebody-help-explain-this-to-me-new-spoiler/385723/6 "2021-06-01T13:19:20Z")

</div>

Closed on June 1, 2021, 1:19pm UTC


