2018-09-30 23:01:58 +01:00
---
id: 56533eb9ac21ba0edf2244b7
title: Concatenating Strings with Plus Operator
challengeType: 1
2019-02-14 12:24:02 -05:00
videoUrl: 'https://scrimba.com/c/cNpM8AN'
2018-09-30 23:01:58 +01:00
---
## Description
< section id = 'description' >
In JavaScript, when the < code > +< / code > operator is used with a < code > String< / code > 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.
< strong > Example< / strong >
2019-05-17 06:20:30 -07:00
```js
'My name is Alan,' + ' I concatenate.'
```
2018-09-30 23:01:58 +01:00
< strong > Note< / strong > < br > Watch out for spaces. Concatenation does not add spaces between concatenated strings, so you'll need to add them yourself.
< / section >
## Instructions
< section id = 'instructions' >
Build < code > myStr< / code > from the strings < code > "This is the start. "< / code > and < code > "This is the end."< / code > using the < code > +< / code > operator.
< / section >
## Tests
< section id = 'tests' >
```yml
2018-10-04 14:37:37 +01:00
tests:
- text: < code > myStr</ code > should have a value of < code > This is the start. This is the end.</ code >
2018-10-20 21:02:47 +03:00
testString: assert(myStr === "This is the start. This is the end.", '< code > myStr< / code > should have a value of < code > This is the start. This is the end.< / code > ');
2018-10-04 14:37:37 +01:00
- text: Use the < code > +</ code > operator to build < code > myStr</ code >
2018-10-20 21:02:47 +03:00
testString: assert(code.match(/(["']).*(["'])\s*\+\s*(["']).*(["'])/g).length > 1, 'Use the < code > +</ code > operator to build < code > myStr</ code > ');
2018-10-04 14:37:37 +01:00
- text: < code > myStr</ code > should be created using the < code > var</ code > keyword.
2018-10-20 21:02:47 +03:00
testString: assert(/var\s+myStr/.test(code), '< code > myStr</ code > should be created using the < code > var</ code > keyword.');
2018-10-04 14:37:37 +01:00
- text: Make sure to assign the result to the < code > myStr</ code > variable.
2018-10-20 21:02:47 +03:00
testString: assert(/myStr\s*=/.test(code), 'Make sure to assign the result to the < code > myStr</ code > variable.');
2018-09-30 23:01:58 +01:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
// Example
var ourStr = "I come first. " + "I come second.";
// Only change code below this line
var myStr;
```
< / div >
### After Test
< div id = 'js-teardown' >
```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
```
< / div >
< / section >
## Solution
< section id = 'solution' >
```js
var ourStr = "I come first. " + "I come second.";
var myStr = "This is the start. " + "This is the end.";
```
< / section >