views:

65

answers:

3

Hi,

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

How do I create a string of spaces, like this:

"          "

in javascript or jQuery from the this array?

Thank you

+4  A: 

You would use Array.join() for this, like this:

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

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

Nick Craver
Had a very basic syntax error, i was joining the array into a string and replacing blanks with '', silly. Thank you very much!
vikp
@downvoter - It helps to say what is wrong, the question is he/she *already has* an array of 10 spaces and wants to join them.
Nick Craver
+1  A: 

You can use join for that. Example:

var x = ['a', 'b', 'c', 'd'];
var y = x.join('');
WoLpH
A: 

Easy, try it yourself in address box:

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

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

Why is this community wiki?
Marcel Korpel