0

JS: Loop in Loop

Hi, I have a question about this piece of code as I can't figure out how it runs. It's a for loop inside a for loop. var y = 0; var x = 0; for (y; y < 3; y++) { for x; x < 3; x++) { console.log(y); } } //output is 0 0 0 1 1 1 2 2 2 What I don't understand here is how it executes, the order of how it runs. I don't want to just remember the code but I want to understand it as well. Thank you very much!

10th Apr 2018, 4:18 AM
ktchpnomm
ktchpnomm - avatar
2 Answers
+ 6
First it goes through one iteration of y, in which it goes to three iterations of x. Then it goes through another iteration of y, in which it goes through another three iterations of x. Last, it goes through one more iteration of y, in which it goes through three iterations of x.
10th Apr 2018, 4:21 AM
Ariela
Ariela - avatar
0
okay, thats very simple and very clear. thank you for helping! :)
10th Apr 2018, 4:25 AM
ktchpnomm
ktchpnomm - avatar