+ operator you use for adding two numbers can be used to concatenate two strings.
You can also use
a += b is a shorthand for
a = a + b.
If the left hand side of the
Array#join() function creates a new string from concatenating all elements in an array. For example:
The first parameter to
join() is called the separator. By default, the separator is a single comma
You can pass in any separator you want. Separators make
Array#join() the preferred choice for concatenating strings if you find yourself repeating the same character over and over again. For example, you can use
' ' as the separator to join an array of words:
Or you can use
'/' to join together URL fragments:
Array#join() a very flexible way to concatenate strings. If you want to join together a variable number of strings, you should generally use
join() rather than a
for loop with
concat() method. The
concat() doesn’t modify the string in place.
The downside of using
concat() is that you must be certain
str1 is a string. You can pass non-string parameters to
concat(), but you will get a TypeError if
str == null.
concat() function is rarely used because it has more error cases than the
+ operator. For example, you would get unexpected behavior if you call
concat() on a value that happens to be an array. You should use
+ instead of
concat() unless you have a very good reason.
If you must use
concat(), it is usually best to call it on an empty string: