javascript concatination

What is the most common, and accepted best practice for concatinating two strings in javascript.  Hopefully most common & best practice use are the same thing ;)

thanks
LVL 1
tf2012Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dimmergeekCommented:
var str3 = str1 + str2;

Open in new window


If you need to put a space between them:
var str3 = str1 + " " + str2;

Open in new window


If you need to put a blank line between them:
var str3 = str1 + "\n" + str2;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Rainer JeschorCommented:
Hi,
dimmergeek is right - but only if both variables are really of type "string".

The dumb prove would be:
var str3 = '' + str1 + str2; 

Open in new window


because Javascript will automatically convert a number to string if one operand is of type string.

And the "nice" way is using the built-in concat function:
http://www.w3schools.com/jsref/jsref_concat_string.asp

HTH
Rainer
0
dimmergeekCommented:
What is the most common, and accepted best practice for concatinating two strings in javascript

I assumed by the user's question that he/she was truly dealing with two strings.
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

GaryCommented:
If you are going to do
var str3 = str1 + str2;

it is just as easy to do
str1 += str2

if you just need a string all together, if you need to keep the individual values separate then use the other two concat methods already mentioned.

No one method is really better than the other,
0
tf2012Author Commented:
I used this format, var str3 = str1 + " " + str2;

thank you!
0
dimmergeekCommented:
Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.