How to change a char in a string without var

I need to know how to change a specific char in a string without using a varable. I know that you can edit the char with the variable but the project im doing it will not work if i do that. Can you please tell me if you know how to do that?
thanks, koji

Technically you cannot change a char in string in JS. In JS, strings are immutable - they cannot be changed. You have to create a new string and store it in the old location - that is why you would have to use var or let. If you used const, it could never be changed.

1 Like

Hey @kojicephas,

As @kevinSmith said, technically, it is not possible to mutate a string. But there are ways to modify it using a different variable. So answering your question, there is no way to modify specific character in a string.

There are a couple of ways to do it, but this is one:

var myStr = '12345';
// 12345

myStr = myStr.substr(0, 2) + 'X' + myStr.substr(3);
// 12X45

But the important thing here is that we are creating a whole new string. It doesn’t work that way in every language, but it does in JS.