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  Divide a number into X parts in JavaScript

Leave a Reply

Your email address will not be published. Required fields are marked *

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