150

How can I append a word to an already populated string variable with spaces?

laalto
  • 150,114
  • 66
  • 286
  • 303
Ronal
  • 2,233
  • 6
  • 21
  • 24

4 Answers4

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
8
var str1 = "add";
str1 = str1 + " ";

Hope that helps,

Dan

Daniel Elliott
  • 22,647
  • 10
  • 64
  • 82
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