Sobes.tech
Junior — Middle

Is it possible to modify an existing string in JavaScript and how?

sobes.tech AI

Answer from AI

In JavaScript, strings are immutable. This means that you cannot change a specific character in an existing string directly. If you need to modify a string, a new string is created based on the existing one with the desired changes.

Example:

let str = "hello";
// You cannot do str[0] = 'H'; // this won't work

// Instead, create a new string:
str = 'H' + str.slice(1); // "Hello"

Thus, string modification occurs through creating a new string, not by changing the existing one.