Formation of string through loops in JavaScript

Loops can create strings. For example, let's make a string filled with ten letters 'x':

let str = ''; for (let i = 0; i < 10; i++) { str += 'x'; } console.log(str); // shows 'xxxxxxxxxx'

And now let's make the string '12345'. To do this, we will add a loop counter to our variable:

let str = ''; for (let i = 1; i <= 5; i++) { str += i; } console.log(str); // shows '12345'

Loop to make the string filled with 5 dashes.

Loop to make the string '123456789'.

Loop to make the string '987654321'.

Loop to form the string '-1-2-3-4-5-6-7-8-9-'.

enru