How to create a string of spaces ("") in javascript or jQuery?

I have an array containing 10 elements, each element contains a " " .

How to create a string of spaces, for example:

  "" 

in javascript or jQuery from this array?

thanks

+4
source share
3 answers

You would use Array.join() , for example:

 var myArray = [" "," "," "," "," "," "," "," "," "," "]; var myString = myArray.join(''); //mySting is a string of 10 spaces 

You need to pass '' to .join() , because the default collector is a comma.

+8
source

Easy, try it yourself in the address field:

 javascript:alert('"'+new Array(42).join(' ')+'"') 

By the way, "in jquery" should be "using jquery"

+23
source

You can use the connection for this. Example:

 var x = ['a', 'b', 'c', 'd']; var y = x.join(''); 
+2
source

Source: https://habr.com/ru/post/1316646/


All Articles