2018-10-10 18:03:03 -04:00
---
id: 56533eb9ac21ba0edf2244ba
2021-02-06 04:42:36 +00:00
title: Understand String Immutability
2018-10-10 18:03:03 -04:00
challengeType: 1
2020-04-29 18:29:13 +08:00
videoUrl: 'https://scrimba.com/c/cWPVaUR'
forumTopicId: 18331
2021-01-13 03:31:00 +01:00
dashedName: understand-string-immutability
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, `String` values are < dfn > immutable</ dfn > , which means that they cannot be altered once created.
2020-12-16 00:37:30 -07:00
2021-02-06 04:42:36 +00:00
For example, the following code:
2020-04-29 18:29:13 +08:00
```js
var myStr = "Bob";
myStr[0] = "J";
```
2021-02-06 04:42:36 +00:00
cannot change the value of `myStr` to "Job", because the contents of `myStr` cannot be altered. Note that this does *not* mean that `myStr` cannot be changed, just that the individual characters of a < dfn > string literal</ dfn > cannot be changed. The only way to change `myStr` would be to assign it with a new string, like this:
2020-04-29 18:29:13 +08:00
```js
var myStr = "Bob";
myStr = "Job";
```
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
Correct the assignment to `myStr` so it contains the string value of `Hello World` using the approach shown in the example above.
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 `Hello World` .
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert(myStr === 'Hello World');
2018-10-10 18:03:03 -04:00
```
2021-02-06 04:42:36 +00:00
You should not change the code above the specified comment.
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert(/myStr = "Jello World"/.test(code));
2018-10-10 18:03:03 -04:00
```
2021-01-13 03:31:00 +01:00
# --seed--
## --after-user-code--
```js
(function(v){return "myStr = " + v;})(myStr);
```
## --seed-contents--
```js
// Setup
var myStr = "Jello World";
// Only change code below this line
myStr[0] = "H"; // Change this line
// Only change code above this line
```
2020-12-16 00:37:30 -07:00
# --solutions--
2020-04-29 18:29:13 +08:00
2021-01-13 03:31:00 +01:00
```js
var myStr = "Jello World";
myStr = "Hello World";
```