Concatenating Strings with the Plus Equals Operator
We can also use the +=
operator to concatenate a string onto the end of an existing string variable. This can be very helpful to break a long string over several lines.
Note: Watch out for spaces. Concatenation does not add spaces between concatenated strings, so you'll need to add them yourself.
Example:
let ourStr = "I come first. ";
ourStr += "I come second.";
ourStr
now has a value of the string I come first. I come second.
.
Build myStr
over several lines by concatenating these two strings: This is the first sentence.
and This is the second sentence.
using the +=
operator. Use the +=
operator similar to how it is shown in the example and be sure to include a space between the two strings. Start by assigning the first string to myStr
, then add on the second string.
Tests
- Waiting: 1.
myStr
should have a single space character between the two strings. - Waiting: 2.
myStr
should have a value of the stringThis is the first sentence. This is the second sentence.
- Waiting: 3. You should use the
+=
operator to buildmyStr
.
/** * Your test output will go here */