Understand String Immutability please

Tell us what’s happening:

Your code so far

// Setup
var myStr = "Jello World";

// Only change code below this line
myStr = "Hello World";
myStr[0] = "H"; // Fix Me


Your browser information:

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

Link to the challenge:
https://www.freecodecamp.org/challenges/understand-string-immutability

Nearly there. Try removing the // fix me

He means to remove that whole line that has “Fix Me” in it.

The point is that in javascript, strings cannot be modified. In a language like C, a statement like:

myStr[0] = 'H';

would be the right way to do it - you can actually change things in the string. (In C, a string is just an array of characters.)

But in JS, you have to replace the whole string - construct a new one and assign it.

1 Like

I Think that’s a Bug

:white_check_mark: I have solved this and I hope it helps you :white_check_mark:


// Setup
var myStr = "Jello World";

// Only change code below this line

myStr = "Hello World";

Bug where? C#, Java, C, C++, JavaScript etc. Be more specific.