-4

EDIT: Looks like I messed up by my method of posting. For future help to others who experience this problem, I will be cleaning up this question.

I've been learning about JavaScript recently and ran into an issue - I believe it's a bug, but I can't find anything about it or how to fix it. For some reason, I get stuck in an infinite for-loop, where I see a constant barrage of '0' instead of the expected '0 1 2'. This is my current setup:

for (var i = 0; i < 2; i = i++) {
    console.log(i)
}

Any help would be much appreciated. For convenience, I took a video of it (20 seconds or so). [I realize that videos do not help those who are trying to help after doing some reading, but I think it could still help those who have the same issue as I did. Therefore I decided to keep the video link just for that reason.]

As extra info, I ran this through Windows 10 WSL (bash for windows), Ubuntu 16.04. I am using Visual Studio Code.

https://photos.app.goo.gl/aJEgY3hEJVTMwRmK6

Thanks ahead of time!

aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • 4
    ...Don't post videos of code or links to videos of code, post the actual code – CertainPerformance Jun 26 '18 at 01:28
  • 2
    `i = i++;` is a no-op. It increments the variable and then immediately resets it back to the previous value. – 4castle Jun 26 '18 at 01:30
  • 1
    [Here are some reasons why posting your code as an image or video is very inconvenient.](//meta.stackoverflow.com/a/285557/) Next time, please post the code and describe the problematic behavior in the question itself. – 4castle Jun 26 '18 at 01:39
  • [Why does this go into an infinite loop?](https://stackoverflow.com/q/3831341/4642212) – Sebastian Simon Jun 26 '18 at 01:40

2 Answers2

0

This behavior is expected:

Increment (++)

The increment operator increments (adds one to) its operand and returns a value.

If used postfix, with operator after operand (for example, x++), then it returns the value before incrementing.

If used prefix with operator before operand (for example, ++x), then it returns the value after incrementing.

The key being for i++ it returns the value before incrementing

The other key being that primitives in JS are immutable, so when you assign a number, a new number is created and held in that variable.

Therefore your loop is saying i = 0, then set i = 0, then the previous 0 is set to 1.

The usual way of doing this kind of loop is to just increment the value without reassignment:

for (var i = 0; i < 2; i++) {
    console.log(i)
}
Community
  • 1
  • 1
coagmano
  • 5,542
  • 1
  • 28
  • 41
0

Remove the i =

Also, you may want to declare the variable i outside of the for, like this:

var i;

for (i = 0; i < 2; i++) {
    console.log(i)
}

The reason I prefer to have the var in the for is that it is not creating a variable limited in scope to the for loop, it will still be at the same level.

user2182349
  • 9,569
  • 3
  • 29
  • 41
  • 1
    There's no reason or need to declare the `i` outside the loop – coagmano Jun 26 '18 at 01:37
  • 1
    As @FredStark noted - using var inside the for statement is fine. The reason I prefer to place it outside is that the declaration of the variable is not limited to the for loop. Edited my answer as well. – user2182349 Jun 26 '18 at 01:44
  • That's a good reason. It is more explicit about the scope in which that variable lives! – coagmano Jun 26 '18 at 01:45