How can I append a word to an already populated string variable with spaces?
Asked
Active
Viewed 4.8e+01k times
4 Answers
344
Like this:
var str = 'blah blah blah';
str += ' blah';
str += ' ' + 'and some more blah';

karim79
- 339,989
- 67
- 413
- 406
26
var str1 = 'abc';
var str2 = str1+' def'; // str2 is now 'abc def'

James Skidmore
- 49,340
- 32
- 108
- 136
-
it's more of a onclick append to variable. can it be done with a single variable? – Ronal Aug 17 '09 at 14:00
-
Sure thing. See my other answer below. – James Skidmore Aug 17 '09 at 14:02
-
@Ronal - if there are any extra bits to your question, you should add them to the question rather than asking within the comments, including any information that might help you get a decent answer. – karim79 Aug 17 '09 at 14:03
5
Ronal, to answer your question in the comment in my answer above:
function wasClicked(str)
{
return str+' def';
}

James Skidmore
- 49,340
- 32
- 108
- 136