Basic JavaScript - Iterate with JavaScript Do...While Loops

In this lesson, the instructions provide the following example:

const ourArray = []; 
let i = 5;

do {
  ourArray.push(i);
  i++;
} while (i < 5);

Then, in the description, it says the following about the above example:

" In this case, we initialize the value of i to 5, just like we did with the while loop. When we get to the next line, there is no condition to evaluate, so we go to the code inside the curly braces and execute it. We will add a single element to the array and then increment i before we get to the condition check. When we finally evaluate the condition i < 5 on the last line, we see that i is now 6, which fails the conditional check, so we exit the loop and are done. At the end of the above example, the value of ourArray is [5] . Essentially, a do...while loop ensures that the code inside the loop will run at least once. Let’s try getting a do...while loop to work by pushing values to an array."

** My Question **
Why does the value of ourArray = [5]? Wouldn’t the value of ourArray = ?
I.E. Wouldn’t the value be empty because it failed the final do… while loop condition:

while (i < 5);

Challenge: Basic JavaScript - Iterate with JavaScript Do…While Loops

Link to the challenge:

do... while loop first executes code within the do block. Only after that the condition is checked. This means the code within do always will run at least once.

In that case, I’m pushing i to the function ourArray. But, I’m also pushing i++ to ourArray. Wouldn’t that mean the value of ourArray is [6]?

When i is pushed its value is 5, increment happens after that, as integers are not mutable in JavaScript, the 5 already in array stays 5.

What you are describing - changing x after its pushed to array, changes it also in array - would apply to mutable types ie. arrays.

const a = [1];
const b = [];
b.push(a);
console.log(b) // [[1]]
a.push(2);
console.log(b) // [[1, 2]];

Hmm. I’m not quite grasping your answer, but I appreciate your response.

When i is pushed its value is 5

This statement makes sense to me.

increment happens after that, as integers are not mutable in JavaScript, the 5 already in array stays 5 .

This statement does not make sense to me.

If my do… while loop is running the code within the code block, I am expecting it to run both lines:

 ourArray.push(i);
 i++;

However, based on what you’re saying, it sounds like since i was pushed into the ourArray first, i cannot be changed any further because integers are immutable. And, only mutable types, such as arrays, can be be changed after the push.

Assuming that’s accurate, then I would have to do my increment before pushing the integer into the array (if my end goal was to make the value of ourArray to be 6).

Is that correct?

And, if I’m understanding all of this correctly, then I think what I’m having difficultly wrapping my mind around is why integers are immutable to begin with, and that’s what I should dig into to study.

That is correct. To have 6 in array, it would need to be incremented before the push. Or manually assigned 6 in place of the 5 later.

Specific reason why some data type is immutable might not be as important as the difference between immutable and mutable.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.