2018-10-10 22:03:03 +00:00
---
id: 56533eb9ac21ba0edf2244b9
2021-02-06 04:42:36 +00:00
title: Constructing Strings with Variables
2018-10-10 22:03:03 +00:00
challengeType: 1
2020-04-29 10:29:13 +00:00
videoUrl: 'https://scrimba.com/c/cqk8rf4'
forumTopicId: 16805
2021-01-13 02:31:00 +00:00
dashedName: constructing-strings-with-variables
2018-10-10 22:03:03 +00:00
---
2020-12-16 07:37:30 +00:00
# --description--
2018-10-10 22:03:03 +00:00
2021-02-06 04:42:36 +00:00
Sometimes you will need to build a string, [Mad Libs ](https://en.wikipedia.org/wiki/Mad_Libs ) style. By using the concatenation operator (`+`), you can insert one or more variables into a string you're building.
Example:
```js
var ourName = "freeCodeCamp";
var ourStr = "Hello, our name is " + ourName + ", how are you?";
// ourStr is now "Hello, our name is freeCodeCamp, how are you?"
```
2018-10-10 22:03:03 +00:00
2020-12-16 07:37:30 +00:00
# --instructions--
2018-10-10 22:03:03 +00:00
2021-02-06 04:42:36 +00:00
Set `myName` to a string equal to your name and build `myStr` with `myName` between the strings `"My name is "` and `" and I am well!"`
2018-10-10 22:03:03 +00:00
2020-12-16 07:37:30 +00:00
# --hints--
2018-10-10 22:03:03 +00:00
2021-02-06 04:42:36 +00:00
`myName` should be set to a string at least 3 characters long.
2018-10-10 22:03:03 +00:00
```js
2020-12-16 07:37:30 +00:00
assert(typeof myName !== 'undefined' & & myName.length > 2);
2018-10-10 22:03:03 +00:00
```
2021-02-06 04:42:36 +00:00
You should use two `+` operators to build `myStr` with `myName` inside it.
2018-10-10 22:03:03 +00:00
```js
2020-12-16 07:37:30 +00:00
assert(code.match(/["']\s*\+\s*myName\s*\+\s*["']/g).length > 0);
2018-10-10 22:03:03 +00:00
```
2021-01-13 02:31:00 +00:00
# --seed--
## --after-user-code--
```js
(function(){
var output = [];
if(typeof myName === 'string') {
output.push('myName = "' + myName + '"');
} else {
output.push('myName is not a string');
}
if(typeof myStr === 'string') {
output.push('myStr = "' + myStr + '"');
} else {
output.push('myStr is not a string');
}
return output.join('\n');
})();
```
## --seed-contents--
```js
// Only change code below this line
var myName;
var myStr;
```
2020-12-16 07:37:30 +00:00
# --solutions--
2020-04-29 10:29:13 +00:00
2021-01-13 02:31:00 +00:00
```js
var myName = "Bob";
var myStr = "My name is " + myName + " and I am well!";
```