Introduction to Strings - What Is a String in JavaScript, and What Is String Immutability?

Tell us what’s happening:

Why do you say that a string is immutable and cannot be changed and then immediately show how it can be changed in the same way a let variable can be re-assigned?

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:145.0) Gecko/20100101 Firefox/145.0

Challenge Information:

Introduction to Strings - What Is a String in JavaScript, and What Is String Immutability?

Hi @bkarlan

Since strings are immutable, we can’t update the first string directly. That is why we are assigning a new string to the developer variable.

let str = 'Hi';
str[1] = 'y';

Since strings a immutable, once they are created, you cannot directly modify them.

The above code will result in an error.

However, the value of a variable can be reassigned.

let one = '1';
one = '2';

The variable one now contains the value '2'.

Happy coding