What is the correct way to concatenate strings in Javascript?

Asked

Viewed 40,567 times

9

I started learning Python and was told that I shouldn’t concatenate the strings with , but use the method .format(). And in Javascript it is correct to use + or , to concatenate the strings or there is a more recommended and more used way?

  • Concatenate in what context? Pure and simple, two strings?

  • That’s all. It’s just that I started to think this is ugly: str1+" "+str2.

  • @bigown I would know if there is any place on the site to post questions half out of scope like this?

  • I wouldn’t say she’s out of scope, maybe too simple :) So it’s right here.

3 answers

16


  • 1

    to not even there with old browsers, to equal the Whatsapp for old cell phones.

  • I just loved these template strings.

12

There’s nothing ugly or wrong in making a simple str1+" "+str2. It can be used without problems. It is simple and efficient. It has no technical reasons to choose a shape, it is just style.

Although ugly depends on taste, I find any other ugly form, longer and even less intuitive when it strikes the eye fast. Pick one and be consistent, don’t keep changing the shape.

There could be problems if you make many concatenations within a loop, but even this JS optimizes, so up to the use of str += str2 is fitting in a loop.

7

  • Which way Voce recommends, Concat or simply the +?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.