Skip to content

Create dynamic variable names inside a JavaScript loop

Learn how to create variable dynamically inside a JavaScript for loop.

for (var i = 1; i <= 5; i++) {
   this["v"+i] = "Variable " + i;
console.log(v1); //Variable 1
console.log(v2); //Variable 2
console.log(v3); //Variable 3
console.log(v4); //Variable 4
console.log(v5); //Variable 5
See also  HackerRank Solutions - Binary Search - Ice Cream Parlor - Java Solution

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.