Problem with var in loop [js] | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Problem with var in loop [js]

hello :) I have a question. How can i solve this problem ? i would to do loop for variable. I don`t wanna writing all the time y1,y2,...y10. var y0 = 0; var y1 = 1 ; var y2 = 2; etc. it's some possibility to write something like this ? because ofc it`s not working. for(i=0;i<3;i++){ var y[i] = i; } I hope someone will help me. Greetings for all of you :)

6th Oct 2017, 12:14 PM
Rybolo
Rybolo - avatar
2 Answers
+ 2
You probably want an array! var y = []; for(i = 0; i < 3; i++){ y[i] = i; } document.write(y[0]); // prints first element
6th Oct 2017, 12:32 PM
Schindlabua
Schindlabua - avatar
+ 1
ya, thanks so much :):)
6th Oct 2017, 12:36 PM
Rybolo
Rybolo - avatar