Does javascript have a method to replace part of a string without creating a new string?

No, strings in JavaScript are immutable.


Not that i am aware of, however if the reason you want to do this is just to keep your code clean you can just assign the new string the the old variable:

var string = "This is a string";
string = string.replace("string", "thing");

Of course this will just make the code look a bit cleaner and still create a new string.


There is a reason why strings are immutable. As Javascript use call-by-sharing technic, mutable string would be a problem in this case :

function thinger(str) {
    return str.replace("string", "thing");
}

var str = "This is a str";
var thing = thinger(str);

In this situation you want your string to be passed by value, but it is not. If str was mutable, thinger would change str, that would be a really strange effect.