String immutability: not an actual property of strings?

Tell us what’s happening:

This is not a specific question about the challenge, but more about the concept of immutability. While working in the “check for palindrome” challenge in the algorithm section, I had to use the string.prototype.replace() function. And that made me come back to this topic, because: Doesn’t the aforementioned function denies the “immutability” property of strings (given that it replaces and/or changes only parts of a string)? I think that it does, but I wanted to consult with the community to be sure.

Thanks in advance!

Your code so far

// Setup
var myStr = "Jello World";

// Only change code below this line

myStr = "Hello World"; // Fix Me


Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0.

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

1 Like

Great question! If you take a look at the documentation for the replace function (see link below), you will see the value returned is a new string, so the original is not modified.

3 Likes

Thanks! That clarifies it completely!