String question

Hello everyone

i have a question about string
let us assume the following code

let x= ‘hi everyone’;

why the follwing code does not change the first letter of the string
x[0]=‘m’;

and i have to use the replace method to make it work
x=x.replace(x.[0],‘m’);

thanks for explaining and have a nice day;

This is an interesting question, which I’d not really thought about the answer to very much.

This section of the String reference page sheds some light on it — https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String#Character_access

You can’t directly write to a string character position using bracket notation, like you can with arrays — string values are immutable. To change one, you need to make a new copy with the changes made.

This Stack Overflow post is also interesting: https://stackoverflow.com/questions/1431094/how-do-i-replace-a-character-at-a-particular-index-in-javascript

thanks again @chrisdavidmills :slight_smile:

have a nice day :slight_smile: