TypeError: Cannot assign to read only property '0' of string 'Jello World'

How do I overcome - TypeError: Cannot assign to read only property ‘0’ of string ‘Jello World’

Your code so far

// Setup

let myStr = "Jello World";

// Only change code below this line

myStr = "Hello World";

myStr[0] = "H"; // Change this line

// Only change code above this line

// Setup
var myStr = "Jello World";

// Only change code below this line
myStr[0] = "Hello World"; // Change this line
// Only change code above this line

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36

Challenge: Understand String Immutability

Link to the challenge:

Strings can’t be changed. You can only make new strings with different contents.

You probably want to slow down and read the lessons more carefully.

In JavaScript, String values are immutable, which means that they cannot be altered once created.

For example, the following code:

let myStr = "Bob";
myStr[0] = "J";

cannot change the value of myStr to Job , because the contents of myStr cannot be altered.

1 Like

HI @aseema.deswal !

Welcome to the forum!

Interestly enough, you do have the correct answer in one of these lines of code but I don’t think you realize which one of these lines of code is the correct answer :grinning:

Let’s take a close look at the example code again so we can better understand the concept.

Here is the lesson code

let myStr = "Bob";
myStr = "Job";

We have a variable called myStr and assigned to it is the name "Bob"
In the second line of code, we are now assigning the word "Job" to the myStr variable.

The same concept applies to the challenge.
We have a variable called let myStr = "Jello World";
But we want to assign a new string to myStr so it says "Hello World"

That is what you will do to this line here

Your answer only need to be one line of code.

Hope that helps!