+ 2

Can I create variable using for loop?

Suppose, I have a word "hello". Now, can I create variable as hello1, hello2, hello3... using a for loop?

17th Jan 2021, 11:04 AM
Name
Name - avatar
6 Answers
+ 4
Well using array variables is kinda like that: e.g. hello[1], hello[2], hello[3] etc.
17th Jan 2021, 11:45 AM
Sonic
Sonic - avatar
+ 4
Name You cannot create variable but you can create key for an object. var a = new Object (); for(var i = 1; i <= 3; i++) { a['word' + i] = i; } console.log(a.word1); console.log(a.word2); console.log(a.word3);
17th Jan 2021, 12:22 PM
A͢J
A͢J - avatar
17th Jan 2021, 11:16 AM
Lisa
Lisa - avatar
+ 2
Name I don't know much Java. Also I don't know what kind of task you are trying to accomplish but generally it is not advisable to create variables at runtime like this (I expressed this worry in the linked thread).
17th Jan 2021, 11:28 AM
Lisa
Lisa - avatar
+ 1
Lisa, I got it thanks... 😊... Can you suggest for java?
17th Jan 2021, 11:18 AM
Name
Name - avatar
+ 1
Name console.log() is javascript
17th Jan 2021, 8:49 PM
Lisa
Lisa - avatar