2018-10-10 18:03:03 -04:00
---
id: 56533eb9ac21ba0edf2244b7
2021-02-06 04:42:36 +00:00
title: Concatenating Strings with Plus Operator
2018-10-10 18:03:03 -04:00
challengeType: 1
2020-04-29 18:29:13 +08:00
videoUrl: 'https://scrimba.com/c/cNpM8AN'
forumTopicId: 16802
2021-01-13 03:31:00 +01:00
dashedName: concatenating-strings-with-plus-operator
2018-10-10 18:03:03 -04:00
---
2020-12-16 00:37:30 -07:00
# --description--
2021-02-06 04:42:36 +00:00
In JavaScript, when the `+` operator is used with a `String` value, it is called the < dfn > concatenation</ dfn > operator. You can build a new string out of other strings by < dfn > concatenating</ dfn > them together.
2020-12-16 00:37:30 -07:00
2021-02-06 04:42:36 +00:00
**Example**
2020-04-29 18:29:13 +08:00
```js
'My name is Alan,' + ' I concatenate.'
```
2021-02-06 04:42:36 +00:00
**Note**
Watch out for spaces. Concatenation does not add spaces between concatenated strings, so you'll need to add them yourself.
Example:
```js
var ourStr = "I come first. " + "I come second.";
// ourStr is "I come first. I come second."
```
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
# --instructions--
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
Build `myStr` from the strings `"This is the start. "` and `"This is the end."` using the `+` operator.
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
# --hints--
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
`myStr` should have a value of `This is the start. This is the end.`
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert(myStr === 'This is the start. This is the end.');
2018-10-10 18:03:03 -04:00
```
2021-02-06 04:42:36 +00:00
You should use the `+` operator to build `myStr` .
2018-10-10 18:03:03 -04:00
```js
2021-02-06 04:42:36 +00:00
assert(code.match(/(["']).*\1\s*\+\s*(["']).*\2/g));
2018-10-10 18:03:03 -04:00
```
2021-02-06 04:42:36 +00:00
`myStr` should be created using the `var` keyword.
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(/var\s+myStr/.test(code));
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
You should assign the result to the `myStr` variable.
2020-04-29 18:29:13 +08:00
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert(/myStr\s*=/.test(code));
2018-10-10 18:03:03 -04:00
```
2020-04-29 18:29:13 +08:00
2021-01-13 03:31:00 +01:00
# --seed--
## --after-user-code--
```js
(function(){
if(typeof myStr === 'string') {
return 'myStr = "' + myStr + '"';
} else {
return 'myStr is not a string';
}
})();
```
## --seed-contents--
```js
var myStr; // Change this line
```
2020-12-16 00:37:30 -07:00
# --solutions--
2021-01-13 03:31:00 +01:00
```js
var myStr = "This is the start. " + "This is the end.";
```