Don't declare a new str variable inside the loop with var str. Reuse the one you declare outside the loop. Also do +=

var divLength = $('div').length;

var str = '';
for(var i = 0; i < divLength; i++) {
  str += "Div #" + i + ", ";
  console.log(str);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div></div>
<div></div>
<div></div>
<div></div>

Answer from Red Mercury on Stack Overflow
🌐
SitePoint
sitepoint.com › javascript
String concatenation in a loop - JavaScript - SitePoint Forums | Web Development & Design Community
March 15, 2018 - take a look at this fiddle. It has a loop that iterates through an object properties…these properties might be true or null. In case they are true I want to construct a string that will be composed of the property names that are true. If update is true then the string will be just update if add is true ALSO then the string would be updateadd.
Top answer
1 of 3
1

Try creating a variable len set to char.length - 1, decrement len within for loop, set data-alphabet to char[len]

$("body").append('<div class="grid"><div class="grid__col--12 lesson blacktxt"></div></div>');

var char = "ابتثجحخدذرزسشصضطظعغفقكلمنهوي";

for (i = 0, len = char.length - 1; i < 28; i++, len--) {
  var $sound = '<nav class="cell"><a class="pointer blacktxt" data-asound="'+ i +'"><i class="fa fa-volume-up"></i></a></nav>'
  var $csound = '<nav class="cell"><input type="text" class="txtcenter" data-csound="'+ i +'" placeholder="'+ i +'" /></nav>'
  var $newDiv = $('<header class="table lessonSec" data-alphabet="'+ char[len] +'"/>').html($sound + $csound)
  $(".lesson").append( $newDiv )
}
body {
  background: #7fb8ff;
}

/* Center DIVs Vertically and Horizontally */
.table {
  display: table;
  width: 100%;
  height: 100%;
  text-align: center;
  margin: 1em 0;
}
.cell {
  display: table-cell;
  vertical-align: middle;
}

/* Global Misc Classes */
.pointer {
  cursor: pointer;
}
.txtcenter {
  text-align: center;
}
<link href="http://treehouse-code-samples.s3.amazonaws.com/poly/css/application.css" rel="stylesheet"/>
<link href="https://fortawesome.github.io/Font-Awesome/assets/font-awesome/css/font-awesome.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

2 of 3
0

Consider this snippet:

var chars = "ابتثجحخدذرزسشصضطظعغفقكلمنهوي";


var arrayOfChars = chars.split('').reverse(); // got them in reverse order

var divs = arrayOfChars.map(buildDiv)

$('.mydiv').prepend(divs)


function buildDiv(c) {
  return $('<div />').attr('data-around', c)
}
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Concatenate a string as many times the for loop runs - JavaScript - The freeCodeCamp Forum
July 15, 2020 - Tell us what’s happening: I want the for loop to run three times(or as many times is specified in the second parameter of the function). For some reason it is not working. I want to concatenate the string as many times it runs in the for loop. Your code so far function repeatStringNumTimes(str, num) { var str1 = str ; if(num
🌐
Mastering JS
masteringjs.io › tutorials › fundamentals › string-concat
3 Ways to Concatenate Strings in JavaScript - Mastering JS
July 29, 2019 - If the left hand side of the + operator is a string, JavaScript will coerce the right hand side to a string.
🌐
Delft Stack
delftstack.com › home › howto › javascript › javascript append to string
How to Append to String in JavaScript | Delft Stack
March 11, 2025 - Can I append strings in a loop? Yes, you can use either the concat() method or the + operator to append strings within a loop, allowing you to build strings dynamically based on certain conditions. Are strings in JavaScript mutable?
🌐
Medium
medium.com › programming-essentials › how-to-concatenate-all-strings-from-an-array-the-bad-and-the-good-way-738909be624d
How to Concatenate All Strings From an Array, The Bad and The Good Way | by Cristian Salcescu | Frontend Essentials | Medium
April 21, 2021 - Below is an example of using a for-loop to build a string. First, we set the computed text as an empty string. Then we iterate over the array and at each step we concatenate a new value to the computed string. const arr = ['Friends', 'Seinfeld', ...
Find elsewhere
🌐
DEV Community
dev.to › tpointtech123 › a-step-by-step-guide-to-string-concatenation-in-javascript-1h3a
A Step-by-Step Guide to String Concatenation in JavaScript - DEV Community
March 20, 2025 - In this example, the += operator is used to append ", " and "world!" to the initial message string. Method 3: Using the concat() Method JavaScript also provides the concat() method, which allows you to concatenate multiple strings.
🌐
TechOnTheNet
techonthenet.com › js › string_concat.php
JavaScript: String concat() method
Let's take a look at an example of how to use the concat() method in JavaScript. ... var totn_string = 'Tech'; console.log(totn_string.concat('On','The','Net')); console.log(totn_string); In this example, we have declared a variable called totn_string that is assigned the string value of 'Tech'.
🌐
Stack Abuse
stackabuse.com › how-to-concatenate-strings-in-an-array-in-javascript
How to Concatenate Strings in an Array in JavaScript
May 16, 2023 - The easiest way to append all elements in an array into one is the join() method of the Array class. It joins all the elements into a string with the given optional delimiter.
🌐
TutorialsPoint
tutorialspoint.com › javascript › string_concat.htm
JavaScript String concat() Method
July 23, 2025 - <html> <head> <title>JavaScript String concat() Method</title> </head> <body> <script> const str1 = "Welcome"; const str2 = "to"; const str3 = "Tutorials"; const str4 = "Point"; document.write("str1 = ", str1); document.write("<br>str2 = ", ...
🌐
W3Schools
w3schools.com › jsref › jsref_concat_string.asp
JavaScript String concat() Method
The concat() method returns a new string. ... let text1 = "Hello"; let text2 = "world!"; let text3 = "Have a nice day!"; let result = text1.concat(" ", text2, " ", text3); Try it Yourself » ... If you want to use W3Schools services as an ...
🌐
Sololearn
sololearn.com › en › Discuss › 2690236 › string-concatenate-loop-in-javascript-
String concatenate loop in JavaScript | Sololearn: Learn to code for FREE!
February 8, 2021 - To add them to a string do somthing like myString+=myArray[using for loop as index]; ... You can also make use of array reduce method. let arr = ["an","im","at","io","ns"]; let c=arr.reduce((a,b)=>{ return a+b; }) console.log(c); ... arr.join('') ...
🌐
JavaScript Tutorial
javascripttutorial.net › home › javascript string methods › string.prototype.concat()
JavaScript String Concat() Method
November 3, 2024 - The following example uses the concat() method to concatenate strings: let greeting = 'Hi'; let message = greeting.concat(' ', 'John'); console.log(message);Code language: JavaScript (javascript)
🌐
Roadmetals
roadmetals.co.nz › tmp › n85we › viewtopic.php
how to append string in javascript using for loop
June 20, 2023 - Browse other questions tagged, Where developers & technologists share private knowledge with coworkers, Reach developers & technologists worldwide, Maybe you were looking for this: as of ES6, there is, as covered in the other answer, you could use str.charAt(i) in place of the []'s. You can initiate many values in expression 1 (separated by comma): And you can omit expression 1 (like when your values are set If the arguments are not of the type string, they are converted to string values before const str = &#x27;Hello' + ' ' + 'World'; str; // 'Hello World' Does the policy change for AI-genera
🌐
n8n
community.n8n.io › questions
Append to a string during a loop - Questions - n8n Community
August 23, 2022 - This is probably really easy but I can’t really find an easy way to do this without a function node (I’m trying to some really simple workflows for studens without using functions). I’ve got a loop (splitInBatches) that…