2018-09-30 23:01:58 +01:00
---
id: 56533eb9ac21ba0edf2244ba
title: Understand String Immutability
challengeType: 1
2019-02-14 12:24:02 -05:00
videoUrl: 'https://scrimba.com/c/cWPVaUR'
2019-07-31 11:32:23 -07:00
forumTopicId: 18331
2021-01-13 03:31:00 +01:00
dashedName: understand-string-immutability
2018-09-30 23:01:58 +01:00
---
2020-11-27 19:02:05 +01:00
# --description--
In JavaScript, `String` values are < dfn > immutable</ dfn > , which means that they cannot be altered once created.
2018-09-30 23:01:58 +01:00
For example, the following code:
2019-05-17 06:20:30 -07:00
```js
var myStr = "Bob";
myStr[0] = "J";
```
2020-11-27 19:02:05 +01: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:
2019-05-17 06:20:30 -07:00
```js
var myStr = "Bob";
myStr = "Job";
```
2020-11-27 19:02:05 +01:00
# --instructions--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
Correct the assignment to `myStr` so it contains the string value of `Hello World` using the approach shown in the example above.
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
2020-11-27 19:02:05 +01:00
`myStr` should have a value of `Hello World` .
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(myStr === 'Hello World');
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
You should not change the code above the specified comment.
2018-09-30 23:01:58 +01:00
```js
2020-11-27 19:02:05 +01:00
assert(/myStr = "Jello World"/.test(code));
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(v){return "myStr = " + v;})(myStr);
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
// Setup
var myStr = "Jello World";
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
// Only change code below this line
myStr[0] = "H"; // Change this line
// Only change code above this line
```
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
var myStr = "Jello World";
myStr = "Hello World";
```