Changing String

could someone tell me what is wrong with my code? thanks

Your code so far


// Setup
var myStr = "Jello World";

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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/604.3.5 (KHTML, like Gecko) Version/11.0.1 Safari/604.3.5.

Challenge: Understand String Immutability

Link to the challenge:

Hey @SARA1985,

 // Setup
var myStr = "Jello World";
// Only change code below this line
myStr[0] = "H"
var myStr = "Jello World";  ⇇⇇
myStr = "Hello World"; 
// Change this line
// Only change code above this line

On the line I marked, you are trying to declare myStr again which is not possible because it’s already declared before it. It will just give you an error saying that the variable is already declared, because it is already declared.

1 Like


Thanks, I have still problem.

I dont think you will be able to index a string. You have to replace the whole string

1 Like

As the challenge states, a string is a primitive value, and primitives are immutable. You cannot change them.

You’ll learn about arrays and objects later on in the curriculum. Those are not primitives, so they can be changed.

This doesn’t work:

var myStr = "Jello";
myStr[0] = "H"

This does work:

var myArray = ["J", "e", "l", "l", "o"];
myArr[0] = "H"

If this doesn’t make much sense now, don’t worry. Just remember that you can’t change/mutate a string, you can only reassign the variable with a new string.

1 Like

myStr[0] = "H" throws an error and stop execuion of the code, you can’t mutate a string like this