2018-09-30 23:01:58 +01:00
---
id: 56533eb9ac21ba0edf2244b8
title: Concatenating Strings with the Plus Equals Operator
challengeType: 1
2019-02-14 12:24:02 -05:00
videoUrl: 'https://scrimba.com/c/cbQmmC4'
2019-07-31 11:32:23 -07:00
forumTopicId: 16803
2021-01-13 03:31:00 +01:00
dashedName: concatenating-strings-with-the-plus-equals-operator
2018-09-30 23:01:58 +01:00
---
2020-11-27 19:02:05 +01:00
# --description--
We can also use the `+=` operator to < dfn > concatenate</ dfn > a string onto the end of an existing string variable. This can be very helpful to break a long string over several lines.
2021-03-02 16:12:12 -08:00
**Note:** Watch out for spaces. Concatenation does not add spaces between concatenated strings, so you'll need to add them yourself.
2020-03-25 08:07:13 -07:00
Example:
```js
2021-10-26 01:55:58 +09:00
let ourStr = "I come first. ";
2020-03-25 08:07:13 -07:00
ourStr += "I come second.";
```
2021-03-02 16:12:12 -08:00
`ourStr` now has a value of the string `I come first. I come second.` .
2020-11-27 19:02:05 +01:00
# --instructions--
2018-09-30 23:01:58 +01:00
2021-08-24 13:26:45 -04:00
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.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --hints--
2018-09-30 23:01:58 +01:00
2021-03-02 16:12:12 -08:00
`myStr` should have a value of the string `This is the first sentence. This is the second sentence.`
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(myStr === 'This is the first sentence. This is the second sentence.');
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
You should use the `+=` operator to build `myStr` .
2018-09-30 23:01:58 +01:00
```js
2020-11-27 19:02:05 +01:00
assert(code.match(/myStr\s*\+=\s*(["']).*\1/g));
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
# --seed--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
## --after-user-code--
2018-09-30 23:01:58 +01:00
```js
2018-10-20 21:02:47 +03:00
(function(){
if(typeof myStr === 'string') {
return 'myStr = "' + myStr + '"';
} else {
return 'myStr is not a string';
}
})();
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
## --seed-contents--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
2021-10-26 01:55:58 +09:00
let myStr;
2020-11-27 19:02:05 +01:00
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --solutions--
2018-09-30 23:01:58 +01:00
```js
2021-10-26 01:55:58 +09:00
let myStr = "This is the first sentence. ";
2018-09-30 23:01:58 +01:00
myStr += "This is the second sentence.";
```