These two operators allow you to join one string to another.
- Concatenate: This refers to appending one string to another.
- Concatenate Assignment: Similar to the previous but refers more specifically to appending a string to the end of a variable. Assigning the final result to that variable.
|+||Concatenate||“str1” + “str2”||String are joined. Results in “str1str2”|
|+=||Concatenate Assignment||str += “str2”||String is appended. str has “str2” appended to it.|
112 to a string.
These examples will give you a rough explanation of the operator and an example of how it is used within actual code.
Concatenate String Operator (
The concatenate operator (
To use this operator, you must specify a string on the left and right sides of the concatenate operator (
a + b
You can also join as many strings together as you would like. Just be wary of memory constraints.
Example of using the Concatenate String Operator
Let us write two quick examples to show you how the concatenate string operator (
- For our second example, we use a string and a number as the two values we want to concatenate.
console.log("Website: " + "pimylifeup.com"); console.log("String " + 123);
Website: pimylifeup.com string 123
Concatenate Assignment String Operator (
+=) allows you to append a string to the end of another string easily. This is useful when you want to add a string onto a string contained within a variable.
This operator is used by specifying a variable, followed by the plus (
+) and equals (
=) sign, then the string you want to be appended to the variable.
var += a
Example of using the Concatenate Assignment String Operator
- We start this example by declaring a variable called “
str” and assigning it the string “
Website:“. This is the variable we will append a string to.
- On the following line, we use the concatenate assignment operator to append the text “
pimylifeup.com” to the end of our “
- We finish the example by using the “
console.log()” function to log the result to the console.
let str = "Website: "; str += "pimylifeup.com"; console.log(str);
These operators allow you to join one string to another with relative ease.