Setting up a little test like this can be helpful to visualize what's going on.
let counter = 2;
let sum = [1, 1];
let i = 1;
let num = 10;
while (counter < num) {
counter = sum[i - 1] + sum[i];
sum.push(counter);
i++;
console.log(
`counter: ${counter}, i: ${i}, num: ${num}, sum: ${sum}, counter < num: ${
counter < num
}`
);
}
If you do this, you'll see the following:
counter: 2, i: 2, num: 10, sum: 1,1,2, counter < num: true
counter: 3, i: 3, num: 10, sum: 1,1,2,3, counter < num: true
counter: 5, i: 4, num: 10, sum: 1,1,2,3,5, counter < num: true
counter: 8, i: 5, num: 10, sum: 1,1,2,3,5,8, counter < num: true
counter: 13, i: 6, num: 10, sum: 1,1,2,3,5,8,13, counter < num: false
The loop will exit when the condition is checked and the result is false. So, the counter will actually change after the last time the condition is true. When counter becomes equal to 13, the condition will no longer be true and after it is checked again, the loop will exit.